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請求的開火(由服務器日誌檢查)。
一下「保存」方法 – sh1ng
你運行通過瀏覽器這個程序或者是你打包起來弄成PhoneGap的網址是什麼?如果您通過瀏覽器運行它,可能會創建一個簡單的索引和js文件,並在方法PUT中使用$ .ajax並調用您的API,以便查看服務器日誌文件。會做一個有趣的測試案例。試圖在Web上看到與Android 2.X和PUT相關的一些問題,但無濟於事。 – TYRONEMICHAEL
我的手機應用程序(*。apk)。我猜這是某種Backbone問題,因爲首次保存/更新產品操作完成成功(帶PUT請求)。 – sh1ng