Backbone.js的 - 不改变的触发而更名 [英] Backbone.js - change not triggering while the name change

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

问题描述

在我的骨干功能,而名字来取得改改功能并不触发..任何一个建议我以正确的方式得到它。(实际上我需要得到改变的东西,需要更新);

code:

 (函数($){VAR列表= {};list.model = Backbone.Model.extend({
  默认值:{
    名称:需要的名字'
  },
  初始化:功能(){
    this.bind('的变化:名称',函数(模型){
        的console.log('型号 - >改变()',模型);
    });
  }
});list.collect = Backbone.Collection.extend({
  型号:list.model,
  网址:数据/ names.json',
  初始化:功能(){
    this.fetch({更新:真正});
    this.keepUpdate();
  },
  keepUpdate:功能(){
    VAR认为这=;
    VAR了updateData =功能(){
      that.fetch({更新:真正});
      myTimeout = setTimeout的(了updateData,10000);
    }
    VAR myTimeout = setTimeout的(了updateData,10000);
  }
});
list.view = Backbone.View.extend({
  初始化:功能(){
    this.collection =新list.collect();
    this.collection.on(更新,this.render,这一点);
    this.collection.bind(改变:名称功能(模型,属性){
      的console.log(模型,属性,财产变'); //这是不是在所有触发..
    });
  },
  渲染:功能(数据){
    _.each(this.collection.models,功能(数据){
      //console.log(data.get('name'));它工作正常
    })
  },
  updateName:功能(){
    的console.log('更新的所谓');
  }
});VAR NewView的=新list.view();})(jQuery的)


解决方案

Col​​lection.fetch不会触发变更事件。你只得到了重置事件。如果您需要更精细的事件,考虑调用 与选项 {更新:真正}

  that.fetch({更新:真正});

这将触发针对已经集合中的每个型号更改事件和添加如果模型是previously不是收藏。

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天全站免登陆