Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ember data noKeyMixin example
var attr = DS.attr,
hasMany = DS.hasMany,
belongsTo = DS.belongsTo,
Model = DS.Model;
App.Address = Model.extend({
street: attr('string'),
streetNumber: attr('string'),
detail: attr('string'),
});
App.Car = Model.extend({
brand: attr('string'),
model: attr('string'),
year: attr('number')
});
App.Person = Model.extend({
firstName: attr('string'),
lastName: attr('string'),
address: belongsTo('address'),
cars: hasMany('car')
});
// To activate the noKeyMixin
App.PersonSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, DS.NoKeyMixin, {
attrs: {
address: {embedded: 'always', noKey: true},
cars: {embedded: 'always', noKey: true}
}
});
//as u can see the noKey affects the serializer and not the model.
//so u can reuse the model via subclassing and made it an
//indipendent resource in the future maybe
//this will generate/parse a request like this
var example = {
id: 1,
fistName: 'Vincenzo',
lastName: 'Ampolo',
address: {
street: 'Via Roma',
streetNumber: 1,
detail: ''
},
cars:[{
brand: 'Alfa Romeo',
year: 2014,
model: 'Giulietta'
},{
brand: 'Ferrari',
year: 2010,
model: 'California'
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment