これは 5 部構成の backbone.js の hello world チュートリアル/アプリケーションです。https://web.archive.org/web/20180317062059/http://arturadib.com/hello-backbonejs/docs/3.htmlパート 3 では、著者がコレクションとモデルを使用してデータを保存する方法と、変更をビューに結び付けます。
この行を除いて、私はそれのほとんどを理解しています
this.collection.bind('add', this.appendItem); // collection event binder
この「バインド」はコンテキストをバインドするだけですか、それともモデルが追加されたときはいつでもappendItemが呼び出されるような「イベント」として機能していますか?
ListView の render メソッドでは、appendItem メソッドを明示的に呼び出しているため、なぜ「add」にバインドされているのでしょうか。
_(this.collection.models).each(function(item){ // in case collection is not empty
self.appendItem(item);
}, this);
誰かがそのコードがどのように機能するかを少し説明してもらえますか? ドキュメントを調べましたが、この方法で使用されるバインドの説明が見つかりませんでした。
完全なコード
(function($){
¶
Item class: The atomic part of our Model. A model is basically a Javascript object, i.e. key-value pairs, with some helper functions to handle event triggering, persistence, etc.
var Item = Backbone.Model.extend({
defaults: {
part1: 'hello',
part2: 'world'
}
});
¶
List class: A collection of Items. Basically an array of Model objects with some helper functions.
var List = Backbone.Collection.extend({
model: Item
});
var ListView = Backbone.View.extend({
el: $('body'),
events: {
'click button#add': 'addItem'
},
¶
initialize() now instantiates a Collection, and binds its add event to own method appendItem. (Recall that Backbone doesn't offer a separate Controller for bindings...).
initialize: function(){
_.bindAll(this, 'render', 'addItem', 'appendItem'); // remember: every function that uses 'this' as the current object should be in here
this.collection = new List();
this.collection.bind('add', this.appendItem); // collection event binder
this.counter = 0;
this.render();
},
render: function(){
¶
Save reference to this so it can be accessed from within the scope of the callback below
var self = this;
$(this.el).append("<button id='add'>Add list item</button>");
$(this.el).append("<ul></ul>");
_(this.collection.models).each(function(item){ // in case collection is not empty
self.appendItem(item);
}, this);
},
¶
addItem() now deals solely with models/collections. View updates are delegated to the add event listener appendItem() below.
addItem: function(){
this.counter++;
var item = new Item();
item.set({
part2: item.get('part2') + this.counter // modify item defaults
});
this.collection.add(item); // add item to collection; view is updated via event 'add'
},
¶
appendItem() is triggered by the collection event add, and handles the visual update.
appendItem: function(item){
$('ul', this.el).append("<li>"+item.get('part1')+" "+item.get('part2')+"</li>");
}
});
var listView = new ListView();
})(jQuery);