2017-08-14 3 views
-1
  toggleCompletedCheck : function(e) { 
      e.stopPropagation(); 
      e.stopImmediatePropagation(); 
      var key = $(e.currentTarget).attr("id"); 
      this.model = todoCollection.findWhere({ 
       key : key 
      }); 
      this.model.toggle("completed", true); 
      this.option.collection = todoCollection.add(this.model); 
      var email = this.model.get("email"); 
      var title = this.model.get("title"); 
      var key = this.model.get("key"); 
      var status = this.model.get("status"); 
      var completed = this.model.get("completed"); 
      this.updateUserData(email, key, title, completed, status); 
      returnValue = this.model.save(); 
      console.log(returnValue); 
     }, 

準備完了状態はまだ関数内にあります。私が使用した変数はウィンドウオブジェクト(returnValue)です。私はコンソール(クロムブラウザから)でオブジェクトを再び印刷すると、ready状態4を示して、returnValue.responseTextを使ってresponseTextにアクセスすることもできます。 backbone.jsを使用して入力をバックエンドに保存しています。これは、responseTextを保存済みとして返します。しかし、順番に、私はそれをしようとすると私はそれにアクセスすることができませんが未定義と言う。この関数で必要としたresponseTextを取得する方法。準備完了状態はまだ1ですが応答はあります。

答えて

1

バックボーンのmodel.save()メソッドは非同期です。値(javascript xhrオブジェクト)を返しますが、要求は戻り時に完全ではありません。

完了応答を使用するには、通常save法(docs here)にsuccessまたはerrorコールバックを渡します

this.model.save(null, { 
    success: function(model, response, options) { 
     // do something with the response 
    }, 
    error: function(model, response, options) { 
     // do something with the response 
    } 
}); 

あなたがからreturn INGの応答に使用されているとき、これは調整のビットすることができあなたの関数を呼び出すことができますが、コールバックを使用して同等の機能がほぼ常に可能です。

+0

ありがとうございました。データはJSONではないためです。私は次のように使いました。 this.model.save([]、{ \tデータ型: "テキスト"、 \t成功:関数(応答){ のreturnValue = response.changed.Saved.xhr.responseText; \t \t \t \t \t にconsole.log (returnValue); \t return returnValue; \t}、 エラー:function(){} }); –

関連する問題