2

モデルの変更をリッスンするようにビューを作成しました。モデルに変更があると、レンダリング関数が呼び出され、警告ウィンドウが表示されます。しかし、2回来ているということは、2つの変更イベントのためにrender関数が2回呼び出されていることを意味します。

WineDetails ビュー
app.WineView = Backbone.View.extend({

    template:_.template($('#tpl-wine-details').html()),

    initialize:function () {
        this.model.bind("change", this.render, this);

    },
    render:function (eventName) {
  if(eventName)alert("changed")
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },

     events:{
        "change input":"change",
        "click .save":"saveWine",
        "click .delete":"deleteWine"
    },
    change:function (event) {
        var target = event.target;
        console.log('changing ' + target.id + ' from: ' + target.defaultValue + ' to: ' + target.value);
        // You could change your model on the spot, like this:
        // var change = {};
        // change[target.name] = target.value;
        // this.model.set(change);
    },
    saveWine:function () {
        this.model.set({
            name:$('#name').val(),
            grapes:$('#grapes').val(),
            country:$('#country').val(),
            region:$('#region').val(),
            year:$('#year').val(),
            description:$('#description').val()
        });
        if (this.model.isNew()) {
            var self = this;
         app.router.wineList.create(this.model,{wait:true,success:function(){
                 app.router.navigate('wines/'+self.model.id,false);
         }});//add event,request event on collection will be triggered

        } else {
            this.model.save();//change event,request event on model will be triggered
        }
        return false;
    },
onClose:function()
    {
        alert("onclose");
        this.model.unbind("change",this.render);
    }

私はこの次のコードを持っているので、それはゾンビビューのせいではありません

Backbone.View.prototype.close=function()
{
    alert("closing view "+this);
    if(this.beforeClose){
        this.beforeClose();
    }
    this.remove();
    this.unbind();
    if(this.onClose){
        this.onClose();
    }

} 

このコードのどこが間違っているか教えてください。ありがとう :)

4

2 に答える 2

7

そのため、通話に関する情報を提供していないため、Model#saveそれがあなたの見解の範囲内のものであると想定します。また、古い方法に従っているため、問題はゾンビのビューに起因するものではないと仮定します。ここで、おそらく何が起こっているかを推測します。

this.model.set({
  name:$('#name').val(),
  grapes:$('#grapes').val(),
  country:$('#country').val(),
  region:$('#region').val(),
  year:$('#year').val(),
  description:$('#description').val()
});
// ...
this.model.save();

わかりました、最初の部分 (setメソッド) は最初のchangeイベントをトリガーします。
2 番目の部分であるsaveメソッド、別の変更をトリガーする可能性があります。もう 1 つsetは、サーバーから返された属性を使用して実行されます。

考えられる問題の解決策:
save属性を渡すことができ、サーバーが応答するまでメソッドwaitの使用を延期するフラグを指定できます。set

this.model.save({
  name:$('#name').val(),
  grapes:$('#grapes').val(),
  country:$('#country').val(),
  region:$('#region').val(),
  year:$('#year').val(),
  description:$('#description').val()
}, {wait: true});
于 2013-05-28T11:39:45.530 に答える
2

次のようなモデルの新しいインスタンスを常に作成して試すこともできます。

var wine = new WineModel({
    name:$('#name').val(),
    grapes:$('#grapes').val(),
    country:$('#country').val(),
    region:$('#region').val(),
    year:$('#year').val(),
    description:$('#description').val()
});

そして、次のように保存します:

wine.save(null, success: function(model){
    // do your call action on call back
},
beforeSend: function() {
    // before save 
}
error: function(model, errors) {
    // on error occurred
});
于 2015-04-22T07:04:13.510 に答える