简体   繁体   中英

Can I pass additional parameters to Ember-Model belongsTo?

I need to pass additional parameters to Ember-Model's belongsTo. I got it to work with its own attribute type. But I need to have relations as well. Is that possible somehow?

UserType = {
  serialize: function(objectId) {
    return {
      __type: 'Pointer',
      className: '_User',
      objectId: objectId
    };
  },

  deserialize: function(object) {
    return object.objectId;
  }
};

App.Movie = Ember.Model.extend({
  objectId: Ember.attr(),

  createdAt: Ember.attr(),
  updatedAt: Ember.attr(),

  owner: Ember.attr(UserType),
  // owner: Ember.belongsTo('App.User', {
  //   key: 'objectId'
  // }),
});

App.User = Ember.Model.extend({
  objectId: Ember.attr(),
  username: Ember.attr(),
  email: Ember.attr(),

  createdAt: Ember.attr(),
  updatedAt: Ember.attr(),
});

App.User.primaryKey = 'objectId';

And here is the JSON of a movie:

{
  "ratings": [
    {
      "objectId": "AQWYN32UUo",
      "value": 4
    }
  ],
  "title": "Inception",
  "watched": true,
  "year": "2010",
  "owner": {
    "__type": "Pointer",
    "className": "_User",
    "objectId": "AQWYN32UUo"
  },
  "createdAt": "2013-11-28T15:38:40.730Z",
  "updatedAt": "2013-11-28T15:55:37.114Z",
  "objectId": "0x5tdCcWiy"
},

Thank you

I see what you're talking about now, you'll need to override the getBelongsTo and s

This is the default implementation

Ember.Model.reopen({
  getBelongsTo: function(key, type, meta) {
    var idOrAttrs = get(this, '_data.' + key),
        record;

    if (Ember.isNone(idOrAttrs)) {
      return null;
    }

    if (meta.options.embedded) {
      var primaryKey = get(type, 'primaryKey');
      record = type.create({ isLoaded: false });
      record.load(idOrAttrs[primaryKey], idOrAttrs);
    } else {
      record = type.find(idOrAttrs);
    }

    return record;
  }
});

serializeBelongsTo: function(key, meta) {
  if (meta.options.embedded) {
    var record = this.get(key);
    return record ? record.toJSON() : null;
  } else {
    var primaryKey = get(meta.getType(), 'primaryKey');
    return this.get(key + '.' + primaryKey);       
  }
}

You could change it to something like this

owner: Ember.belongsTo('App.User', {key: 'owner', serializer:UserType}),


Ember.Model.reopen({
  getBelongsTo: function(key, type, meta) {
    var idOrAttrs = get(this, '_data.' + key),
        record;

    if (Ember.isNone(idOrAttrs)) {
      return null;
    }

    // THIS IS NEW
    if (meta.options.serializer){
      idOrAttrs = meta.options.serializer.deserialize(idOrAttrs);
    }

    if (meta.options.embedded) {
      var primaryKey = get(type, 'primaryKey');
      record = type.create({ isLoaded: false });
      record.load(idOrAttrs[primaryKey], idOrAttrs);
    } else {
      record = type.find(idOrAttrs);
    }

    return record;
  },

  serializeBelongsTo: function(key, meta) {
    if (meta.options.embedded) {
      var record = this.get(key);
      return record ? record.toJSON() : null;
    } else {

      // THIS IS NEW
      if(meta.options.serializer){
        return meta.options.serializer.serialize(record);
      }
      var primaryKey = get(meta.getType(), 'primaryKey');
      return this.get(key + '.' + primaryKey);
    }
  }


});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM