hereにあるBackboneJsで見つけたチュートリアルに従っています。彼は次のことを行いrender
方法で半押しについてBackboneJsチュートリアルの質問
:
events: { 'click button#add': 'addItem' },
initialize: function() {
this.collection = new List();
// Collection event binder
this.collection.bind('add', this.appendItem);
this.counter = 0;
this.render();
},
render: function() {
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
appendItem(item);
}, this);
},
addItem: function() {
var item = new Item();
this.counter++;
item.set({
part2: item.get('part2') + " " + this.counter
});
this.collection.add(item);
},
appendItem: function (item) {
$('ul').append('<li>' + item.get('part1') + " " + item.get('part2') + '</li>');
}
私は下の行についての質問のカップルを持っています。
_(this.collection.models).each(function(item){
// in case collection is not empty
appendItem(item);
}, this);
アンダースコア_
はこのコンテキストで何をしていますか?
これはなぜ必要なのでしょうか?
コメントは、コレクションが空でない場合に表示されます。しかし、その行がなければうまく動作します。そしてinitialize
関数の中のbind
のオーバーライドは、add
イベントがコレクションでトリガされたときにBacklightにthis.appendItem
を実行するように指示するので、問題の行を削除して考えて確認します。
パーフェクト。ありがとう。私はアンダースコアが依存関係であることを完全に忘れていました。私はそれがおそらくバックボーンのことだと思っていました。 –