javascript – 在couchdb中使用ember-resource – 如何保存文档?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在couchdb中使用ember-resource – 如何保存文档?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2960字,纯文字阅读大概需要5分钟。
内容图文
我正在使用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(); // Everything OK up to this location
Ember.run.next(this, function() {
this.fetch() // Sub-Document is reset here, and *not* refetched!
.fail(function(error) {
App.displayError(error);
})
.done(function() {
App.log("App.Resource.forceReload fetch done, got revision " + self.get('_rev'));
});
});
}
这适用于大多数情况,但如果我有嵌套模型,则在执行fetch之前,子模型将替换为旧版本的数据!
有趣的是,正确的(更新的)数据存储在数据库中,并且在获取之后错误的(旧的)数据在内存模型中,尽管_rev属性是正确的(以及主对象的所有属性).
这是我的对象定义的一部分:
App.TaskDefinition = App.Resource.define({
url: App.dbPrefix + 'courseware',
schema: {
id: String,
_rev: String,
type: String,
name: String,
comment: String,
task: {
type: 'App.Task',
nested: true
}
}
});
App.Task = App.Resource.define({
schema: {
id: String,
title: String,
description: String,
startImmediate: Boolean,
holdOnComment: Boolean,
..... // other attributes and sub-objects
}
});
任何可能出现问题的想法?
非常感谢任何建议!
亲切的问候,
托马斯
解决方法:
我发现了一个更好的解决方案,可以从数据库中恢复新版本,而无需在每次保存后重新加载数据.
如果在带有更新选项的ember-resource中调用save,则db的响应将合并到资源对象中.对保存请求的couchdb的答案是这样的
{"ok":true,"id":"mydocument-123","rev":"10-5f3cb46df301143a966251148f88663d"}
所以我只是将我的对象的_rev属性设置为db的rev值.
我的应用程序特定资源类作为所有couchdb对象的超类如下:
App.Resource = Ember.Resource.extend({
save: function(options) {
options = options || {};
options.update = true; // sets id and rev after saving in ember-resource; rev is copied to _rev in didSave callback!
this.set('rev', undefined);
return this._super(options);
},
// we get the new revision in the "rev" attribute of the response from the save-request.
// since we set options.update to true, we get this value in the attribute "rev", which we simply copy to "_rev"
didSave: function() {
this._super.apply(this, arguments);
this.set('_rev', this.get('rev'));
}
.... // other properties and functions
});
资源定义如下:
App.TaskExecution = App.Resource.define({
....
schema: {
id: String,
_rev: String,
rev: String,
type: String,
..... // other attributes
}
});
到目前为止工作得很好….
内容总结
以上是互联网集市为您收集整理的javascript – 在couchdb中使用ember-resource – 如何保存文档?全部内容,希望文章能够帮你解决javascript – 在couchdb中使用ember-resource – 如何保存文档?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。