2017-08-14 37 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); 
     }, 

就緒狀態仍爲1與功能。我使用的變量是一個窗口對象(returnValue)。當我在控制檯中再次打印對象時(來自chrome瀏覽器),它顯示我準備就緒狀態4還允許我使用returnValue.responseText訪問responseText。我正在使用backbone.js將輸入保存到後端。這將返回保存的responseText。但是,反過來,當我試圖說未定義時,我無法訪問它。如何獲得我在這個函數中需要的responseText。就緒狀態仍爲1,雖然我得到的迴應

回答

1

骨幹的model.save()方法是異步的。它返回一個值(javascript xhr對象),但請求在返回時沒有完成。

要使用完成響應,則通常會通過successerror回調到save方法(docs here):

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}, error:function(){} }); –

相關問題