我正在使用ember.js和couchdb实现一个应用程序.我选择ember-resource作为数据库访问层,因为它很好地支持嵌套的JSON文档.
由于couchdb在每个文档中使用_rev属性进行乐观锁定,因此在将数据保存到couchdb之后,必须在我的应用程序中更新此属性.
我实现这个的想法是在保存到数据库后立即重新加载数据,然后用文档的其余部分重新获得新的_rev.
这是我的代码:
// Since we use CouchDB, we have to make sure that we invalidate and re-fetch
// every document right after saving it. CouchDB uses an optimistic locking
// scheme based on the attribute "_rev" in the documents, so we reload it in
// order to have the correct _rev value.
didSave: function() {
this._super.apply(this, arguments);
this.forceReload();
},
// reload resource after save is done, expire to make reload really do something
forceReload: function() {
this.expire(); // …Run Code Online (Sandbox Code Playgroud)