Is there a way to call an Ember data function, e.g. the serialize
function, directly inside my main javascript of the index page?
I tried DS.Serializer.serialize(myrecord)
, but I got this error: has no method 'serialize'
.
Additional question:
How can I directly call the RESTAdapter's version of serialize? They have made some modifications to the generic serialize.
Is there a way to call an Ember data function, e.g. the serialize function, directly inside my main javascript of the index page?
Yes and no. Yes in that there is nothing special about ember-data functions (they can be called from anywhere) but no because calling DS.Serializer.serialize(myrecord)
does not make sense.
Probably what you are looking to do is serialize myrecord? In that case try:
myrecord.serialize()
The serialize()
function of an ember-data model will use the serialization stragey of the model's store to return a JSON representation of the model. Under the hood that means calling serialize() on an instance of DS.Serializer. See DS.Model.serialize()
I tried DS.Serializer.serialize(myrecord), but I got this error: has no method 'serialize'
Right. DS.Serializer
is a class and has no serialize
method. So DS.Serializer.serialize(myrecord)
doesn't work. The serialize
function you linked to is an instance method, so it will be available on instances of the DS.Serializer
class. That said, DS.Serializer is an abstract base class so it would not make much sense to create an instance of it and try calling serialize()
on that.
More more info on how ember-data serializers work have a look at the [serializer api docs] (https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/lib/system/serializer.js#L10-L211)
-- UPDATED for Additional question: --
How can I directly call the RESTAdapter's version of serialize? They have made some modifications to the generic serialize.
If using RESTAdapter you could just call model.serialize()
on a model. But in the rare case that your models are using something else (like FixtureAdapter) and you want serialization features of RESTSerializer, you can create a serializer using DS.RESTSerializer.create({})
and then pass your model to its serialize()
method. For example:
App = Ember.Application.create();
App.Store = DS.Store.extend({adapter: 'DS.FixtureAdapter'});
App.Post = DS.Model.extend({
title: DS.attr('string'),
bodyText: DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return App.Post.createRecord({title: 'This is my post', bodyText: 'There are many like it but this one is mine'});
},
afterModel: function(model, transition) {
var serializer = DS.RESTSerializer.create({});
console.log('FixtureSerializer via model.serialize(): ', model.serialize());
console.log('RESTSerializer via serializer.serialize(post):', serializer.serialize(model));
}
});
Console log output will be:
> FixtureSerializer via model.serialize(): Object {title: "This is my post", bodyText: "There are many like it but this one is mine"}
> RESTSerializer via serializer.serialize(post): Object {title: "This is my post", body_text: "There are many like it but this one is mine"}
You can see that RESTSerializer converts the bodyText attribute from camelCase to underscore. Live example is here: http://jsbin.com/uduzin/3/edit