0

私はレシピモデルを持っています、そしてレシピはたくさんの材料を保存する材料リストコレクションを持っています。

フォーム送信から材料リストに材料を追加するとき、サーバーから「id」を取得する必要があるため、ajaxリクエストを実行し、IDを取得して、モデルに材料を追加しようとしています。

私のcomponentlist.viewには、

    初期化:function(){
        this.recipe = this.model;
        }、

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

        成分.fetch({

            成功:function(){
                this.recipe('add:ingredients'、function(ingredient、ingredientlist){

                });
            }、
            エラー:function(){
                new Error({メッセージ: "材料を追加しています"});
            }
        });
        }

'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 を View にバインドする必要があります: bindを見て関数をトリガーしてください!
  • コンテキスト (this) を「get_ingredients」に渡してみてください

これはちょうど私の5セントです

于 2012-01-11T01:00:20.380 に答える