Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

Backbone.js - change not triggering while the name change

In my backbone function, while the name get change the change function not at all triggering.. any one suggest me the right way to get it.. (actually i need to get changed stuff and need to update);

code :

    (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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Collection.fetch doesn't trigger the change event. You only get the reset event. If you need more granular events, consider calling fetch with the options {update:true}.

that.fetch({update:true});

That will trigger change event for every model that was already in the collection, and add if the model was previously not in the collection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...