0

Phonegap + バックボーン + jquery モバイル。私のバックボーンモデルは次のようになります(一部のコードは削除されています)

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 リクエストなしで成功イベントが発生します (サーバー ログで確認)。

4

2 に答える 2

0

バックボーン ソースを見ると、保存しようとしているモデルに ID がある場合にのみ PUT リクエストがトリガーされます。次のことを試していただけますか。

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

コードをざっと見たところ、製品にどのようにデータを入力したかわかりません。最初に取得してから、再保存しようとしていますか? とにかく、バックボーンは Id が設定されていない場合は POST を発行し、Id プロパティが設定されている場合は PUT を発行します。Backbone Docsのように Id Attribute プロパティを変更できます。両方のインスタンスでモデルを console.log し、関連する Id プロパティが設定されていることを確認します。

Android 2.x以降のAndroid 4で動作するのは奇妙です。上記が正しい方向に向かない場合は、製品がどのように設定されているかについて、より多くのコードを提供できるかもしれません。

于 2013-08-05T12:59:16.217 に答える
0

これは android 2 ブラウザの問題です。GET、POST、および PUT(facepalm) リクエストをキャッシュします。私はしたい

jQuery.ajaxSetup({
                 cache: false
            });

ただし、GET リクエストにのみ影響します。

私の最終的な解決策は、URLにナンスを追加することです

url: function(){ return "rest_entity + $.now(); }
于 2013-08-05T21:13:35.287 に答える