Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干:更新不调用回调函数_Backbone.js - Fatal编程技术网

Backbone.js 主干:更新不调用回调函数

Backbone.js 主干:更新不调用回调函数,backbone.js,Backbone.js,我正在尝试将该功能添加到我的应用程序中,以便更新我的数据库,然后更新DOM。数据库得到了很好的更新,但是DOM没有。以下是我的部分观点: App.Views.Table = Backbone.View.extend({ tagName: 'span', initialize: function(options) { this.model.on('change', this.render, this); this.model.on('update'

我正在尝试将该功能添加到我的应用程序中,以便更新我的数据库,然后更新DOM。数据库得到了很好的更新,但是DOM没有。以下是我的部分观点:

 App.Views.Table = Backbone.View.extend({
    tagName: 'span',
    initialize: function(options) {
        this.model.on('change', this.render, this);
        this.model.on('update', this.remove, this);

        this.template = this.options.template;
        this.url = this.options.url;
    },
    events: {
        'click .verify': 'verify',
        'click .spam': 'spam',
        'click .duplicate': 'duplicate'
    },
    verify: function(e) {
        id = e.currentTarget.id;
        table = new App.Models.Table({ id: id });
        table.urlRoot = this.url;
        table.fetch();
        table.toJSON();
        table.set('verified', 1);
        table.save();

    },
    spam: function(e) {
        ...

    },
    duplicate: function(e) {
            ...
    },
    remove: function() {
        this.$el.remove();
        console.log('hello');
    },
    retrieveTemplate: function(model) {
        return _.template($('#' + this.template).html(), model);
    },
    render: function() {
        //console.log(this);
        this.$el.html(this.retrieveTemplate(this.model.toJSON()));
        return this;
    }
});

据我所知,
this.model.on('update',this.remove,this)应在
保存
完成时调用我的删除函数。但是回调没有触发,因为我没有得到
console.log
,我的DOM也没有被更新。我做错了什么?我遵循了一个教程,但是教程中的一切都很好。

没有
更新事件。我想你是说
sync

“同步”(型号、响应、选项)-当型号已成功与服务器同步时。

我还发现了一个有用的调试工具,即使用
all
事件查看触发了哪些事件

编辑:

经过一些调试后,
verify()
函数的目标是将已验证的属性保存到模型中。为此,我们需要将
verify()
更改为

this.model.set('verified', 1);
this.model.save();
而不是创建新的
App.Model.Table
并将其设置为
Table
变量。执行
table
.save()操作时,保存的是新的
模型,而不是旧模型
此.model
。这就是为什么连接到此.model的事件处理程序被触发。

Backbone.js中没有“创建”或“更新”事件。这就是为什么您的remove()回调没有启动

有关主干中可能发生的事件的目录,请参阅

更新

仔细查看代码后,答案很清楚。它们是不同的型号:

initialize: function(options) {
    this.model.on('change', this.render, this);
    this.model.on('sync', this.remove, this);

对象上发生的事件不会触发绑定到完全不同模型(this.model)的事件处理程序

既然已经有了模型,为什么还要创建另一个模型(表)?(这个模型)

*更新*

我真的不明白你想做什么,但也许可以试试这个:

table = new App.Models.Table({ id: id });
table.on('sync', this.remove, this);

我还尝试了
create
而不是
update
,但仍然没有成功。table.toJSON();--顺便说一句,这行代码没有做任何事情。你需要对它返回的JSON做些什么。我尝试了
this.model.on('sync',this.remove,this)
,对吗?它不起作用。当您尝试同步时,可能出现错误?在这种情况下,您需要订阅
error
eventError也不起作用。我可以看到成功的PUT请求。也不会全部触发。一定有更根本的错误吧。。您使用的是0.9.9版吗?如果是这样,我将尝试
all
事件。如果那没有被击中。。。。不确定它可能是什么:(好的。我是否做了
这个.model.on('sync',this.remove,this);
?@sehummel是的,成功保存()应该会触发它。如果它没有被触发,一定是有错误了。有什么想法吗?也许它没有看到模型?@sehummel试试这个:
table.save({},{success:function(){console.log('success');},{error:function(){console.log('error');}}});
来证明它是成功的,这是有意义的。我只是删除了“new”这个词吗?
table = new App.Models.Table({ id: id });
table.on('sync', this.remove, this);