Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top