Guest User

Untitled

a guest
Dec 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. // **This example introduces two new Model actions (swap and delete), illustrating how such actions can be handled within a Model's View.**
  2. //
  3. // _Working example: [5.html](../5.html)._
  4.  
  5. //
  6. var _tmp = function($){
  7. // `Backbone.sync`: Overrides persistence storage with dummy function. This enables use of `Model.destroy()` without raising an error.
  8. Backbone.sync = function(method, model, success, error){
  9. success();
  10. }
  11.  
  12. var Item = Backbone.Model.extend({
  13. defaults: {
  14. part1: 'hello',
  15. part2: 'world'
  16. }
  17. });
  18.  
  19. var List = Backbone.Collection.extend({
  20. model: Item
  21. });
  22.  
  23. var ItemView = Backbone.View.extend({
  24. tagName: 'li', // name of tag to be created
  25. // `ItemView`s now respond to two clickable actions for each `Item`: swap and delete.
  26. events: {
  27. 'click span.swap': 'swap',
  28. 'click span.delete': 'remove'
  29. },
  30. // `initialize()` now binds model change/removal to the corresponding handlers below.
  31. initialize: function(){
  32. _.bindAll(this, 'render', 'unrender', 'swap', 'remove'); // every function that uses 'this' as the current object should be in here
  33.  
  34. this.model.bind('change', this.render);
  35. this.model.bind('remove', this.unrender);
  36. },
  37. // `render()` now includes two extra `span`s corresponding to the actions swap and delete.
  38. render: function(){
  39. $(this.el).html('<span style="color:black;">'+this.model.get('part1')+' '+this.model.get('part2')+'</span> &nbsp; &nbsp; <span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;">[swap]</span> <span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;">[delete]</span>');
  40. return this; // for chainable calls, like .render().el
  41. },
  42. // `unrender()`: Makes Model remove itself from the DOM.
  43. unrender: function(){
  44. $(this.el).remove();
  45. },
  46. // `swap()` will interchange an `Item`'s attributes. When the `.set()` model function is called, the event `change` will be triggered.
  47. swap: function(){
  48. var swapped = {
  49. part1: this.model.get('part2'),
  50. part2: this.model.get('part1')
  51. };
  52. this.model.set(swapped);
  53. },
  54. // `remove()`: We use the method `destroy()` to remove a model from its collection. Normally this would also delete the record from its persistent storage, but we have overridden that (see above).
  55. remove: function(){
  56. this.model.destroy();
  57. }
  58. });
  59.  
  60. // Because the new features (swap and delete) are intrinsic to each `Item`, there is no need to modify `ListView`.
  61. var ListView = Backbone.View.extend({
  62. el: $('body'), // el attaches to existing element
  63. events: {
  64. 'click button#add': 'addItem'
  65. },
  66. initialize: function(){
  67. _.bindAll(this, 'render', 'addItem', 'appendItem'); // every function that uses 'this' as the current object should be in here
  68.  
  69. this.collection = new List();
  70. this.collection.bind('add', this.appendItem); // collection event binder
  71.  
  72. this.counter = 0;
  73. this.render();
  74. },
  75. render: function(){
  76. $(this.el).append("<button id='add'>Add list item</button>");
  77. $(this.el).append("<ul></ul>");
  78. _(this.collection.models).each(function(item){ // in case collection is not empty
  79. appendItem(item);
  80. }, this);
  81. },
  82. addItem: function(){
  83. this.counter++;
  84. var item = new Item();
  85. item.set({
  86. part2: item.get('part2') + this.counter // modify item defaults
  87. });
  88. this.collection.add(item);
  89. },
  90. appendItem: function(item){
  91. var itemView = new ItemView({
  92. model: item
  93. });
  94. $('ul', this.el).append(itemView.render().el);
  95. }
  96. });
  97.  
  98. var listView = new ListView();
  99. }
  100. _tmp(jQuery);
Add Comment
Please, Sign In to add comment