-1

私のバックボーン関数では、名前が変更されている間、変更関数はまったくトリガーされません..誰もがそれを取得する正しい方法を提案してくれます..(実際には、変更されたものを取得し、更新する必要があります);

コード:

    (function($){

var list = {};

list.model = Backbone.Model.extend({
  defaults:{
    name:'need the name'
  },
  initialize:function(){
    this.bind('change:name', function(model) {
        console.log('Model->change()', model);
    });
  }
});

list.collect = Backbone.Collection.extend({
  model:list.model,
  url : 'data/names.json',
  initialize:function(){
    this.fetch({update:true});
    this.keepUpdate();
  },
  keepUpdate:function(){
    var that = this;
    var updateData = function(){
      that.fetch({update:true});
      myTimeout = setTimeout(updateData,10000);
    }
    var myTimeout = setTimeout(updateData,10000);
  }
});


list.view = Backbone.View.extend({
  initialize:function(){
    this.collection = new list.collect();
    this.collection.on("update", this.render, this);
    this.collection.bind("change:name", function(model, attributes){
      console.log(model,attributes,'property changed'); // this is not triggering at all..
    });
  },
  render:function(data){
    _.each(this.collection.models, function(data){
      //console.log(data.get('name')); it works fine
    })
  },
  updateName:function(){
    console.log('updated called');
  }
});

var newView = new list.view();    

})(jQuery)
4

3 に答える 3

1

changeCollection.fetch はイベントをトリガーしません。イベントのみを取得しresetます。より詳細なイベントが必要な場合はfetch、 options を使用して呼び出すことを検討して{update:true}ください。

that.fetch({update:true});

changeこれにより、すでにコレクションに含まれていたすべてのモデルのイベントがトリガーされadd、モデルが以前にコレクションに含まれていなかった場合にトリガーされます。

于 2013-01-22T07:17:02.897 に答える
0

あなたの質問を理解しているかどうかわかりません。何を達成しようとしていますか?

fetch がパラメーターとして受け入れられるとは思いません{add:true}(ソース コードを確認したところ、どこにも表示されません)。

が完了すると、イベントfetchのみがトリガーされます( ではありません)。コレクションの内容が変わったときに何かをしたい場合は、それを聞く必要があります。簡単に聞くこともできます。resetaddchange

于 2013-01-22T15:13:08.903 に答える
0

コレクションから keepUpdate を削除し、最後にビューの初期化関数に setTimeout を入れてみてください。コレクションの初期化関数の代わりに、ビューと this.collection.fetch() から fetch を呼び出すことをお勧めします。コードをより再利用可能にします。

于 2013-01-22T09:22:56.233 に答える