2014-02-28 157 views
0

我开始使用骨干网和这里位于jsfiddler我的代码:添加模型Backbone.Collection不刷新视图

http://jsfiddle.net/KZyeV/2/

正如你所上的按钮,我想的点击见新增车辆:

addVehicle: function(){ 
     console.log(this.collection); 
     this.collection.add({Color:'black', Type: 'bicycle'}); 
     //this.render(); why do I have to again call render? without this it doesn't work. 
    } 

但由于某些原因,除非我称之为this.render();不更新视图。这是正确的方法吗?我曾与Knockout合作过,这不是那么有效。模型更新后,视图会自动更新,但由于某种原因,视图在此处不更新。

+0

'this.collection.bind(“reset add”,this.render,this);' – pawel

回答

2

只需添加:

initialize: function(){    
    this.template = _.template($('#vehicleItemTemplate').html()); 
    this.collection.bind("reset", this.render, this); 
    this.collection.bind("add", this.render, this); 
}, 

骨干不会自动发生反应,收集相关的改变。如果你想要的话,看一下像Marionette.js这样的库(它建立在Backbone之上)。

+0

很酷!谢谢大卫! – Jack