在 ajax 事件中将模型添加到关系模型集合
我有一个食谱模型,并且食谱有一个成分列表集合,其中存储了一堆成分。
当我从表单提交中将成分添加到成分列表时,我必须从服务器获取“id”,因此我执行 ajax 请求,获取 id,然后尝试将成分添加到模型中。
在我的成分列表.视图中,我
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" }); } }); }
没有包含触发“get_ingredient”的函数,因为我得到的ajax很好,所以问题不在于触发“get_ingredient”。
我收到错误Uncaught TypeError: Property 'recipe' of object [object DOMWindow] is not a function
使用现有代码。
完成这样的事情的最好方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我也是backbone.js 的新手!
所以我的想法是:
这只是我的 5 美分
First of All i'm a newbie too with backbone.js!
So my thoughts is :
This is just my 5 cents