2013-08-06 37 views
1

我很奇怪,爲什麼我不能做到以下幾點:煎茶觸摸model.save()和更新,無需額外負荷

newChallenge = Ext.create('App.model.User', {name:'donald'}); 
newChallenge.save(); 
*** After another user action *** 
newChallenge.set('name','george'); 
newChallenge.save(); 

我遇到的問題是,第二保存/更新甚至沒有觸發AJAX補丁/張貼到服務器後第一個,並沒有設置名稱爲'喬治'

我沒有看到日誌中的任何錯誤或數據庫中的更新。

型號:

Ext.define('App.model.User', { 
    extend: 'Ext.data.Model', 
    requires: [ 
     'Ext.data.identifier.Uuid' 
    ], 
    config: { 
     identifier: 'uuid', 
     fields: [ 
      { name: 'id', type: 'auto', persist: false }, 
      { name: 'name', type: 'string' } 

     ]   
     proxy: { 
      type: 'rest', 
      api: { 
       create: App.util.Config.getApiUrl('user_profile'), 
       update: App.util.Config.getApiUrl('user_profile'), 
       read: App.util.Config.getApiUrl('user_profile') 
      }, 
      reader: { 
       type: 'json' 
      }, 
      writer: { 
       type: 'json-custom-writer-extended', 
       writeAllFields: true, 
       nameProperty: 'mapping' 
      } 
     } 
    } 
}); 

服務器響應(TastyPie):

{ 
    "name":"george", 
    "id":35, 
    "resource_uri":"/app/api/1/user/35/", 
    "start_date":"2013-08-06T14:49:11.030298" 
} 

謝謝你,史蒂夫

回答

0

你忘了說明什麼問題,你不?你知道,我期望的是,我得到的是...

無論如何,你的代碼被破壞,因爲它依賴於save()的調用是同步的(意味着第一個調用會阻塞直到響應被接收和處理) ,但事實並非如此。你應該開始修正:

newChallenge = Ext.create('App.model.User', {name:'donald'}); 
newChallenge.save({ 
    success: function(model) { 
     newChallenge.set('name','george'); 
     newChallenge.save(); // you should handle error here too 
    } 
    // you should handle error cases too... 
}); 
+0

感謝rixo。我根據你的建議更新了這個問題。第二次保存是在第二次提交後發生的,所以同步阻塞不是問題。錯誤情況也在那裏,爲了清晰起見,僅僅省略了。 – daxiang28