2013-08-05 15 views
0

Phonegap + Backbone + jquery mobile。 我的骨幹機型看起來像(一些代碼刪除)Backbone.js保存模型不會導致android 2.x上的ajax PUT請求

var Purchase = Backbone.Model.extend({ 
    defaults: function() { 
     return { 
      name: "", 
      desc: "", 
      shopping: null, 
      price:null, 
      quantity:1, 
      createdAt: new Date().getTime(), 
      modifiedAt: new Date().getTime() 
     }; 
    } 
}); 

var PurchaseList = Backbone.Collection.extend({ 
    model: Purchase, 
    comparator: 'name' 
}); 

var ShoppingList = Backbone.Model.extend({ 
    defaults: function(){ 
     return { 
      name:"", 
      totalPrice: 0, 
      createdAt: new Date().getTime(), 
      modifiedAt: new Date().getTime(), 
      products: new PurchaseList() 
     }; 
    } 
}); 

更新購買邏輯重新計算ShoppingList的totalPrice

save: function(position){ 

      var data = this.$el.find("form").serializeObject(); 
      var price = parseFloat(data.price); 
      var quantity = parseFloat(data.quantity); 
      var product = new Purchase(data); 
      product.on('invalid', this.showError); 

      var shopping = GlobalShopping.findWhere({id: data.shopping}); 

      if(!isNaN(price) && !isNaN(quantity)) 
        shopping.set('totalPrice', shopping.get('totalPrice') + price * quantity); 

      if(!isNaN(price)) 
        product.set('price', price); 
      else 
        product.unset('price'); 

      if(!isNaN(quantity)) 
        product.set('quantity', quantity); 

      if(this.model.id){ 

        var oldProduct = shopping.get('products').findWhere({id: this.model.id}); 

        if(oldProduct.get('price') != null) 
          shopping.set('totalPrice', shopping.get('totalPrice') - oldProduct.get('price') * oldProduct.get('quantity')); 

        product.id = this.model.id; 
        product.save({}, { success: function(){ 
            shopping.get('products').add([product], {merge: true}); 

            shopping.save({}, {success: function(){ // <<<< issue here 
              window.app_router.navigate('#shopping/' + data.shopping, {replace: true, trigger: true});  
          }}); 

        }}); 
      } else { 
        shopping.get('products').create(product, {wait: true, success: function(){ 
          shopping.save({}, {success: function(){ 
              window.app_router.navigate('#shopping/' + data.shopping, {replace: true, trigger: true});  
        }}); 

      }, error: function(model, xhr, options){ 
        alert(JSON.stringify(product.toJSON())); 
      }});  
      } 
    } 

此代碼的工作完美於Android 4.x的,但在Android 2.x的成功事件沒有PUT請求的開火(由服務器日誌檢查)。

回答

0

這是android 2瀏覽器問題。它緩存GET,POST和PUT(facepalm)請求。 我想

jQuery.ajaxSetup({ 
       cache: false 
      }); 

但它隻影響GET請求。

我最後的解決辦法是添加隨機數以

url: function(){ return "rest_entity + $.now(); } 
0

如果您查看骨幹源代碼,只有在您嘗試保存的模型有Id時纔會觸發PUT請求。您可以嘗試以下操作:

product.set('id', 1); 
product.save(); 

快速瀏覽一下您的代碼,我不確定您是如何填充您的產品的。你是第一次獲取它然後試圖重新保存它?無論如何,Backbone將在沒有設置Id的情況下發出POST,如果Id屬性被設置,則會發出PUT。您可以像Backbone Docs那樣更改您的Id屬性。我會在兩個實例中使用console.log模型,並確保設置相關的Id屬性。

奇怪的是它適用於Android 2.x及以上版本的Android 4。也許你可以提供更多關於如何設置產品的代碼,如果上述不能讓你朝着正確的方向發展。

+0

一下「保存」方法 – sh1ng

+0

你運行通過瀏覽器這個程序或者是你打包起來弄成PhoneGap的網址是什麼?如果您通過瀏覽器運行它,可能會創建一個簡單的索引和js文件,並在方法PUT中使用$ .ajax並調用您的API,以便查看服務器日誌文件。會做一個有趣的測試案例。試圖在Web上看到與Android 2.X和PUT相關的一些問題,但無濟於事。 – TYRONEMICHAEL

+0

我的手機應用程序(*。apk)。我猜這是某種Backbone問題,因爲首次保存/更新產品操作完成成功(帶PUT請求)。 – sh1ng

相關問題