Ember-data and MongoDB, how to handle _id

后端 未结 9 1957
眼角桃花
眼角桃花 2021-02-08 17:15

I\'m using ember-data with rails and MongoDB and am having problem with the way IDs are stored in MongoDB - in a _id field.

Ember-data will use id as the default field f

9条回答
  •  一向
    一向 (楼主)
    2021-02-08 17:39

    I had a similar problem using ember.js with ember-resource and couchdb, which also stores it's IDs as _id.

    As solution to this problem I defined a superclass for all my model classes containing a computed property to duplicate _id into id like this:

    // get over the fact that couchdb uses _id, ember-resource uses id
    id: function(key, value) {
        // map _id (couchdb) to id (ember)
        if (arguments.length === 1) {
            return this.get('_id');
        }
        else {
            this.set('_id', value);
            return value;
        }
    }.property('_id').cacheable()
    

    Maybe this could solve your problem too?

提交回复
热议问题