Javascript 与couchdb一起使用ember资源-如何保存文档?

Javascript 与couchdb一起使用ember资源-如何保存文档?,javascript,couchdb,ember.js,Javascript,Couchdb,Ember.js,我正在使用ember.js和couchdb实现一个应用程序。我选择ember资源作为数据库访问层,因为它很好地支持嵌套的JSON文档 由于couchdb在每个文档中使用属性_rev进行乐观锁定,因此在将数据保存到couchdb后,必须在我的应用程序中更新该属性 我的想法是在保存到数据库后立即重新加载数据,并将新版本与文档的其余部分一起返回 以下是我的代码: // Since we use CouchDB, we have to make sure that we invalidate and r

我正在使用ember.js和couchdb实现一个应用程序。我选择ember资源作为数据库访问层,因为它很好地支持嵌套的JSON文档

由于couchdb在每个文档中使用属性_rev进行乐观锁定,因此在将数据保存到couchdb后,必须在我的应用程序中更新该属性

我的想法是在保存到数据库后立即重新加载数据,并将新版本与文档的其余部分一起返回

以下是我的代码:

// 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'));
            });
    });
}
这适用于大多数情况,但如果我有一个嵌套模型,则在执行获取之前,子模型将替换为数据的旧版本

有趣的是,正确的(更新的)数据存储在数据库中,错误的(旧的)数据在获取后存储在内存模型中,尽管_revattribute是正确的(以及主对象的所有属性)

以下是我的对象定义的一部分:

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
}
});
你知道问题出在哪里吗

谢谢你的建议

亲切问候,,
Thomas

我找到了一个更好的解决方案,可以从数据库中获取新版本,而不必在每次保存后重新加载数据

如果使用更新选项在
ember resource
中调用
save
,则db的响应将合并到资源对象中。couchdb对save请求的回答如下

{"ok":true,"id":"mydocument-123","rev":"10-5f3cb46df301143a966251148f88663d"}
因此,我只是将对象的
\u 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
    }

});

到目前为止效果很好……

您好,您是否已经找到了解决此问题的方法?如果我不能正确运行这个项目,我可能也会这样做。与此同时,我找到了一个更好的解决方案。请看下面我的答案。谢谢更新!这似乎是一个很好的解决方案+我想我也会尝试类似的更新。