【发布时间】:2015-02-21 14:51:14
【问题描述】:
这不是关于我对 Backbone 架构理解的具体但常见的问题。
所以我需要直接从列表中编辑产品属性。 我为产品列表创建了一个主干模型和一个主干集合。
在列表中您可以选择任何模型,系统应向下滑动此模型的编辑界面。
因此,我启动了要在其中呈现列表并显示/编辑集合中的特定模型的视图:
App.Views.ProductList = Backbone.View.extend({
el: '#content',
initialize: function() {
this.render();
},
events: {
'click #show': 'show', // to show model interface in the list
'click #save': 'save' // to save model changes in the list
},
show: function(e) {
var id = $(e.currentTarget).data('id');
$('#product' + id).slideToggle();
},
save: function(e) {
var id = $(e.currentTarget).data('id');
var form = $('#product' + id + ' form');
var model = this.collection.at(id);
model.set(form.serializeJSON());
model.save({
url: '/product'
});
},
render: function() {
var template = _.template($('#productList').html());
this.$el.empty().append(
template({
products: this.collection.toJSON()
})
);
return this;
}
});
所以,伙计们,我确信在从其集合中更新模型的方式中,我采用了非常非常错误的模式。我认为集合能够监听集合模型的变化。
我选择 model.save() 的方式使用集合 url 而不是模型 url,我不能覆盖集合 url。请解释一下你将如何解决这个问题!
【问题讨论】:
标签: javascript backbone.js underscore.js