我有一个食谱模型,一个食谱有一个成分列表集合,其中存储了一堆成分。
当我从表单提交中将成分添加到成分列表时,我必须从服务器获取“id”,因此我执行 ajax 请求,获取 id,然后尝试将成分添加到模型中。
在我的配料表视图中,我有
初始化:函数(){ this.recipe = this.model; }, get_ingredient:功能(成分){ var ingredients_id = new MyApp.Models.Ingredient; 成分.url='/ingredients/?ing='+encodeURIComponent(ingredient_array[i]); 成分.fetch({ 成功:函数(){ this.recipe('add:ingredients', function(ingredient,ingredientlist){ }); }, 错误:函数(){ 新错误({消息:“添加成分”}); } }); }
我没有包含触发“get_ingredient”的函数,因为我可以很好地处理ajax,所以问题不在于触发“get_ingredient”。
我Uncaught TypeError: Property 'recipe' of object [object DOMWindow] is not a function
使用现有代码得到错误。
完成这样的事情的最佳方法是什么?