backbone.js nested collection, add event fires, but returns parent model

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?


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?

链接地址: http://www.djcxy.com/p/48312.html

上一篇: 从集合中删除绑定以从视图中删除

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