backbone.js 嵌套集合,添加事件触发,但返回父模型
我一直在尝试将集合嵌套在模型中。 我有一个食谱,一个食谱有成分列表(集合),其中有成分(模型)。
我首先尝试了主干关系模型,但随后选择了此处提供的方法 backbone.js 构建嵌套视图和模型
当我向集合添加成分时,会触发添加事件。
initialize: function(){ recipe = this.model; console.log(recipe); _.bindAll(this,"add","remove"); recipe.ingredientlist.each(this.add); recipe.ingredientlist.bind('add', this.add); recipe.ingredientlist.bind('remove', this.remove); this.render(); }, add: function(ingredient){ console.log(ingredient); }
但在我的控制台中,当我尝试输出添加的成分时,我得到了返回的配方模型。
我的模型看起来像这样,
MyApp.Models.Recipe = Backbone.Model.extend({ initialize: function(){ this.ingredientlist = new MyApp.Collections.IngredientList(); this.ingredientlist.parent = this; });
如何获得绑定以返回刚刚添加到集合中的成分,而不是整个配方模型?
I've been trying to nest a collection inside a model.
I've got a recipe, and a recipe has ingredientslist(collection) which has ingredient(model).
I first tried backbone relational-model, but then opted for the method provided here backbone.js structuring nested views and models
When I add an ingredient to the collection, the add event is triggered.
initialize: function(){ recipe = this.model; console.log(recipe); _.bindAll(this,"add","remove"); recipe.ingredientlist.each(this.add); recipe.ingredientlist.bind('add', this.add); recipe.ingredientlist.bind('remove', this.remove); this.render(); }, add: function(ingredient){ console.log(ingredient); }
but in my console, where I am trying to output the ingredient which was added, I'm getting the recipe model returned.
My model looks like this
MyApp.Models.Recipe = Backbone.Model.extend({ initialize: function(){ this.ingredientlist = new MyApp.Collections.IngredientList(); this.ingredientlist.parent = this; });
how do I get the bind to return the ingredient which was just added to the collection, rather than the entire recipe model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试重新创建您的代码: http://jsfiddle.net/UVYDv/ 并且,尽我所能告诉你,它按预期工作。也许模型创建有问题?
I tried to recreate your code: http://jsfiddle.net/UVYDv/ and, as far as I can tell, it works as intended. Maybe a problem with the creation of the models?