backbone.js 嵌套集合,添加事件触发,但返回父模型

发布于 2024-12-26 09:41:43 字数 960 浏览 0 评论 0原文

我一直在尝试将集合嵌套在模型中。 我有一个食谱,一个食谱有成分列表(集合),其中有成分(模型)。

我首先尝试了主干关系模型,但随后选择了此处提供的方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2025-01-02 09:41:43

我尝试重新创建您的代码: 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文