backbone.js - model.save() 的更改事件在主干中触发两次

标签 backbone.js

我已经创建了 View 来监听模型的变化。当模型发生变化时,将调用渲染函数并提示警告窗口。但是它来了两次,这意味着渲染函数由于两个更改事件而调用了两次。

WineDetails View
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);
    }

这不是因为僵尸 View ,因为我有以下代码

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

} 

请告诉我这段代码有什么问题。谢谢你:)

最佳答案

因此,由于您没有提供有关您的 Model#save 调用的信息,我假设它是您认为的那个。我还假设问题不是来自僵尸 View ,因为您使用的是过时的方法。我将在这里猜测可能发生的事情:

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 事件。
第二部分,save 方法可能触发另一个变化。另一个 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});

关于backbone.js - model.save() 的更改事件在主干中触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16790319/

相关文章:

javascript - Backbone js批处理保存

javascript - _.pluck 的 Backbone 集合不起作用

javascript - Backbone.js 我们可以没有任何 tagName 吗?

javascript - 当跟进请求的答案是 204 时,为什么 OPTIONS 请求会失败?

javascript - 在 Backbone js View 中分配 ' this' 上下文

javascript - 如何使用 Backbone 在模板中将 <option> 标记为已选择

javascript - EJS(嵌入式 javascript)与backbone.js——差异快速总结

javascript - UnderscoreJS _.filter 通过多个 BackboneJS 集合

backbone.js - 在 Backbone Collection 上调用 invoke

javascript - BackboneJS。 View 加载后如何将纪元图添加到 View 中