2012-05-04 18 views
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は呼び出されません。何がうまくいかないでしょうか?

答えて

1

バックボーンコレクションのフェッチコールは、リセットイベントを発生させます。代わりにバインドしてリセットしてください。

http://documentcloud.github.com/backbone/#Collection-fetch

+0

あなたは{追加:真}合格しない限り、オプションで取得するために、それはアイテムを追加(および火災のイベントを「追加」)、その場合には、コレクションにはなく、それをリセットします。 – jimr

関連する問題