Backbone.js的:有没有从去年的服务器保存更改? [英] backbone.js: Is there a change since last server-save?

查看:98
本文介绍了Backbone.js的:有没有从去年的服务器保存更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个骨干网模型。
随着model.set()我可以设置一个本地值,具有model.save()我可以整个模型保存到服务器。

I have a backbone-Model. With model.set() I can set a local value, with model.save() I can save the whole model to the server.

我怎么知道,是否有改变自上次服务器保存意味着本地版本是脏的。

How do I know, whether there was a change since the last server-save meaning the local version is dirty.

model.isNew();作品只有当模型从未保存到服务器

model.isNew(); works only if the model has never been saved to the server.

推荐答案

编辑::此答案1.0版本骨干之前写的。由于当前版本骨干(1.2.2) hasChanged 不再反映自上次保存的转变。相反,它反映的变化从去年的设置


This answer was written prior to the 1.0 version of Backbone. As of the current Backbone version (1.2.2) hasChanged no longer reflects "since last save" changes. Instead, it reflects changes "since last set".

监听更改事件或检查 hasChanged

如果该模式发生了变化,可以节省的变化。你甚至可以连线的保存方法火的时候改变事件发生。

If the model has changed, you can save on change. You can even wire your save method to fire when the change event happens.

如果您不想保存更改,则设置属性为模型被弄脏并清除它,当你明确地保存。

If you don't want to save on change, then set a property for the model being dirty and clear it when you explicitly save.

是这样的:

change: function(){
    this.dirty = true;
}

save: function(){
    // do your save
    if(success){
        this.dirty = false;
    }
}

isDirty: function(){
    return this.dirty
}

这篇关于Backbone.js的:有没有从去年的服务器保存更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆