0

I have a recipe model, and a recipe has an ingredientlist collection which stores a bunch of ingredients.

When I add an ingredient to the ingredient list from a form submit, I have to get an 'id' from the server, so I do an ajax request, get the id, and am trying to then add the ingredient to the model.

In my ingredientlist.view, I have

    initialize: function(){
        this.recipe = this.model;
        },

         get_ingredient: function(ingredient){
            var ingredient_id = new MyApp.Models.Ingredient;
        ingredient.url='/ingredients/?ing='+encodeURIComponent(ingredient_array[i]);

        ingredient.fetch({

            success: function() {
                this.recipe('add:ingredients', function(ingredient,ingredientlist){

                });
            },
            error: function() {
                new Error({ message: "adding ingredient" });
            }
        });
        }

I didn't include the function which triggers the 'get_ingredient', because it I am getting the ajax fine, so the problem isn't in triggering the 'get_ingredient'.

I get the errorUncaught TypeError: Property 'recipe' of object [object DOMWindow] is not a function using the existing code.

what is the best way to accomplish something like this?

pedalpete
  • 21,076
  • 45
  • 128
  • 239

1 Answers1

1

First of All i'm a newbie too with backbone.js! So my thoughts is :

  • U need to bind your get_ingredient in your View : look bind to trigger your functions!
  • Try to pass the Context (this) to "get_ingredients"

This is just my 5 cents

3logy
  • 2,634
  • 8
  • 46
  • 99
  • I'm not sure what you mean about passing `Context(this)`, can you elaborate? maybe some sample code? I have bound the `get_ingredient` in the view's initialize statement, but when I try to output `console.log(this.recipe)`, I still get undefined. – pedalpete Jan 11 '12 at 11:57
  • How are you passing your Model to the View? you must test if the Model is really passed for example: in the Router( if you use one)!! – 3logy Jan 11 '12 at 12:10
  • yeah, I'm passing it to the view through the router. In my initialize statement in the view, I have `console.log(this.model)`, and that spits out the model no problem, and the model has the structure of relational, so that isn't the problem either. – pedalpete Jan 11 '12 at 14:55
  • I removed the $.ajax for the more backbone-ish .fetch, and my error has changed now, but the issue seems the same, I can't add a model to the collection. I've updated the question as well. – pedalpete Jan 11 '12 at 15:20
  • 1
    I decided to give [http://stackoverflow.com/questions/6353607/backbone-js-structuring-nested-views-and-models/#answer-6476507](this way) a try, and it worked on the first go (for the most part). I think it may be easier to stick with the more conventional backbone methods for now. Thanks for your help. – pedalpete Jan 11 '12 at 17:35