Ember-data and MongoDB, how to handle _id

后端 未结 9 832
别跟我提以往
别跟我提以往 2021-02-08 17:20

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:32

    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?

提交回复
热议问题