更新集合中的模型
我的问题是如何更新集合中的模型?这就是我正在做的事情。在页面加载时,我获取联系人列表。在一个视图中,我在无序列表中列出了这些联系人。每个联系人都是可点击的,这将带您进入编辑表单。对联系人进行更改后,您可以保存该联系人。这将带您到将更改后的模型保存回集合中的方法。你会怎么做?在主干文档中没有更新方法(或者至少我没有看到它)。我创建了一种方法来执行此操作,但我不确定它是否是首选的骨干方式。在这里:
updatePlan : function()
{
//unique ID of the model
var id = $( 'input[ name=id ]' ).val();
//remove the old model from the collection
this.collection.remove( this.model );
//add the updated model to the collection
this.collection.add( this.model );
}
您会认为会有这样的功能:
updatePlan : function()
{
this.collection.update( this.model );
}
谢谢您的帮助
My question is how would you update a model in a collection? Here is what I am doing. On page load I fetch a list of contacts. In one view I list out these contacts in an unordered list. Each contact is clickable which will take you to an edit form. Once you make changes to the contact you can save the contact. This will take you to a method that save the altered model back to the collection. How would you do this? In the backbone docs there isn't an update method (or at least I don't see it). I created a way to do this but I am not sure if it is the preferred Backbone way. Here it is:
updatePlan : function()
{
//unique ID of the model
var id = $( 'input[ name=id ]' ).val();
//remove the old model from the collection
this.collection.remove( this.model );
//add the updated model to the collection
this.collection.add( this.model );
}
You would think there would be a function like this:
updatePlan : function()
{
this.collection.update( this.model );
}
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让您的视图允许用户编辑联系人并为您更新模型。由于模型是通过引用传递的,因此它也将在它所属的集合中进行更新。
这是一个例子。
然后,当您创建编辑视图时,您从集合中传入选定的模型。
剩下要做的唯一一件事就是当用户单击“保存”按钮时将模型保存到服务器。
You can have your view that allows the user to edit the contact update the model for you. And since the model gets passed by reference, it will also be updated in the collection that it is a part of.
Here's an example.
And then when you create the edit view, you pass in the selected model from the collection
The only thing left to do is to save the model to the server when the user clicks the save button.
我同意保罗的观点,你应该有一个用于你的收藏的视图,一个用于你的模型的视图。
模型的视图允许您编辑模型,并且保存应该正确传播。
不过,这表明您似乎有一个视图,其中有 1 个模型和 1 个集合(基于您的删除和添加 this.model),因此您有一个显示单个模型的视图...我认为:D
然后您可以这样做这。
我认为问题可能只是您最初的“逻辑”,您不编辑集合中的模型。
您编辑一个模型,该模型恰好属于一个集合。
因此,模型和 model.collection 都会触发事件,就像在您的 id 集上一样,Change:id 事件将触发并触发到您的模型及其集合的任何绑定
希望它有帮助:)
I agree with Paul, you should have a View for your collection, and a View for your model.
The view for the model allows you to edit your model and the save should propagate up correctly.
However that said you seem to have a view there that has 1 model and 1 collection (based on your remove and add this.model) so you have a view that is displaying a single model... I assume :D
You could then do this.
I think the issue might then just be your initial "logic", you don't edit a model within a collection.
You edit a model, that model just happens to belong to a collection.
So events will fire for both the model and the model.collection, like on your set of id, the Change:id event will fire and trigger any binding to either your model its collections
Hope it helps :)