Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone tutorial: model.destroy + remove and unrender functions
    primarykey
    data
    text
    <p>I am trying to understand this sample backbone codes from this <a href="http://arturadib.com/hello-backbonejs/docs/5.html" rel="nofollow">tutorial</a>, but I can't get my head around why we need this is <code>dummy function</code> - any ideas what it is for?</p> <pre><code> Backbone.sync = function(method, model, success, error){ success(); } </code></pre> <p>Then, <code>remove</code> and <code>unrender</code> functions seem to be the same - why do we need both of them?</p> <p>And what is this line for,</p> <pre><code>this.model.bind('remove', this.unrender); </code></pre> <p>while we have bound the <code>remove</code> function and the click event to the dom?</p> <p>also, we already have bound all functions (including <code>unrender</code>) in this line,</p> <pre><code>_.bindAll(this, 'render', 'unrender', 'swap', 'remove'); </code></pre> <p>so why do we need this line below?</p> <pre><code>this.model.bind('remove', this.unrender); </code></pre> <p>You can read the entire code from that tutorial above, here is some of it,</p> <pre><code>// `Backbone.sync`: Overrides persistence storage with dummy function. This enables use of `Model.destroy()` without raising an error. Backbone.sync = function(method, model, success, error){ success(); } var Item = Backbone.Model.extend({ defaults: { part1: 'hello', part2: 'world' } }); var List = Backbone.Collection.extend({ model: Item }); var ItemView = Backbone.View.extend({ tagName: 'li', // name of tag to be created // `ItemView`s now respond to two clickable actions for each `Item`: swap and delete. events: { 'click span.swap': 'swap', 'click span.delete': 'remove' }, // `initialize()` now binds model change/removal to the corresponding handlers below. initialize: function(){ _.bindAll(this, 'render', 'unrender', 'swap', 'remove'); // every function that uses 'this' as the current object should be in here this.model.bind('change', this.render); this.model.bind('remove', this.unrender); }, // `render()` now includes two extra `span`s corresponding to the actions swap and delete. render: function(){ $(this.el).html('&lt;span style="color:black;"&gt;'+this.model.get('part1')+' '+this.model.get('part2')+'&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span class="swap" style="font-family:sans-serif; color:blue; cursor:pointer;"&gt;[swap]&lt;/span&gt; &lt;span class="delete" style="cursor:pointer; color:red; font-family:sans-serif;"&gt;[delete]&lt;/span&gt;'); return this; // for chainable calls, like .render().el }, // `unrender()`: Makes Model remove itself from the DOM. unrender: function(){ $(this.el).remove(); }, // `swap()` will interchange an `Item`'s attributes. When the `.set()` model function is called, the event `change` will be triggered. swap: function(){ var swapped = { part1: this.model.get('part2'), part2: this.model.get('part1') }; this.model.set(swapped); }, // `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). remove: function(){ this.model.destroy(); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload