2
私はBackbone.jsを使い始めていますが、もっとも簡単な概念実証を得ることはできません。フェッチ時に 'add'イベントが発生しないようです。
$(function() {
var Contact = Backbone.Model.extend({
url: 'contacts.txt'
});
var ContactList = Backbone.Collection.extend({
model: Contact,
url: 'contacts.txt'
});
var Contacts = new ContactList();
var ContactView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#contact-template').html()),
initialize: function() {
_.bindAll(this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var AppView = Backbone.View.extend({
el: '#contacts',
initialize: function() {
Contacts.bind('add', this.addOne, this);
Contacts.fetch();
},
addOne: function(Contact) {
var view = new ContactView({model: Contact});
this.$el.append(view.render().el);
}
});
var app = new AppView();
});
ファイルcontacts.txt
はChromeに係る微細ロードされる単純なJSON構造、含まれています:何らかの理由で
[{"from_acct_id": 5, "ct": 0, "from_display_nm": "Name 1", "to_acct_id": 1},
{"from_acct_id": 3, "ct": 1, "from_display_nm": "Name 2", "to_acct_id": 1},
{"from_acct_id": 2, "ct": 0, "from_display_nm": "Name 3", "to_acct_id": 1},
{"from_acct_id": 4, "ct": 1, "from_display_nm": "Name 4", "to_acct_id": 1}]
を、内add
イベントにバインドされたaddOne()
機能を、私は次のコードを持っていますAppView
は呼び出されません。何がうまくいかないでしょうか?
あなたは{追加:真}合格しない限り、オプションで取得するために、それはアイテムを追加(および火災のイベントを「追加」)、その場合には、コレクションにはなく、それをリセットします。 – jimr