DS.Model#eachRelationship()

eachRelationship (callback, binding)

Inherited from DS.Model but overwritten in addon/-private/system/relationships/ext.js:603

Given a callback, iterates over each of the relationships in the model, invoking the callback with the name of each relationship and its relationship descriptor.

The callback method you provide should have the following signature (all parameters are optional):

function(name, descriptor);
  • name the name of the current property in the iteration
  • descriptor the meta object that describes this relationship

The relationship descriptor argument is an object with the following properties.

  • key String the name of this relationship on the Model
  • kind String "hasMany" or "belongsTo"
  • options Object the original options hash passed when the relationship was declared
  • parentType DS.Model the type of the Model that owns this relationship
  • type String the type name of the related Model

Note that in addition to a callback, you can also pass an optional target object that will be set as this on the context.

Example

app/serializers/application.js
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  serialize: function(record, options) {
    var json = {};

    record.eachRelationship(function(name, descriptor) {
      if (descriptor.kind === 'hasMany') {
        var serializedHasManyName = name.toUpperCase() + '_IDS';
        json[serializedHasManyName] = record.get(name).mapBy('id');
      }
    });

    return json;
  }
});

Parameters:

callback Function
the callback to invoke
binding Any
the value to which the callback's `this` should be bound
doc_EmberJs
2016-11-30 16:50:08
Comments
Leave a Comment

Please login to continue.