0

我有一个食谱模型,一个食谱有一个成分列表集合,其中存储了一堆成分。

当我从表单提交中将成分添加到成分列表时,我必须从服务器获取“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 使用现有代码得到错误。

完成这样的事情的最佳方法是什么?

4

1 回答 1

1

首先,我也是backbone.js的新手!所以我的想法是:

  • 你需要在你的视图中绑定你的 get_ingredient :看起来绑定来触发你的函数!
  • 尝试将上下文(this)传递给“get_ingredients”

这只是我的 5 美分

于 2012-01-11T01:00:20.380 回答