Backbone.js - 名称更改时不会触发更改 [英] Backbone.js - change not triggering while the name change

查看:15
本文介绍了Backbone.js - 名称更改时不会触发更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主干函数中,虽然名称得到更改,但更改函数根本不会触发..任何人都建议我正确的方法来获取它..(实际上我需要更改内容并需要更新);

代码:

 (function($){变量列表 = {};list.model = Backbone.Model.extend({默认值:{name:'需要名字'},初始化:函数(){this.bind('change:name', function(model) {console.log('Model->change()', 模型);});}});list.collect = Backbone.Collection.extend({模型:list.model,url : '数据/名称.json',初始化:函数(){this.fetch({update:true});this.keepUpdate();},保持更新:功能(){var that = this;var updateData = function(){that.fetch({update:true});myTimeout = setTimeout(updateData,10000);}var myTimeout = setTimeout(updateData,10000);}});list.view = Backbone.View.extend({初始化:函数(){this.collection = new list.collect();this.collection.on("更新", this.render, this);this.collection.bind("change:name", function(model, attributes){console.log(model,attributes,'property changed');//这根本没有触发..});},渲染:功能(数据){_.each(this.collection.models, function(data){//console.log(data.get('name'));它工作正常})},更新名称:函数(){console.log('更新调用');}});var newView = new list.view();})(jQuery)

解决方案

Collection.fetch 不会触发 change 事件.您只会收到 reset 事件.如果您需要更细粒度的事件,请考虑使用选项 fetch>{update:true}.

that.fetch({update:true});

这将为集合中已经存在的每个模型触发 change 事件,如果模型之前不在集合中,则触发 add 事件.

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)

解决方案

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.

这篇关于Backbone.js - 名称更改时不会触发更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆