How to directly call an Ember data function?

Is there a way to call an Ember data function, for example. serialize function directly inside my main javascript index page?

I tried DS.Serializer.serialize(myrecord) but got this error: has no method 'serialize' .


Additional question:

How can I directly call the serialization RESTAdapter version ? They made some changes to the general serializer .

+3
ember-data
source share
1 answer

Is there a way to call an Ember data function, for example. serialization function directly inside my main javascript index page?

Yes and no. Yes, 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 want to do is serialize myrecord? In this case, try:

 myrecord.serialize() 

The serialize() function of the ember-data data model will use the stragey serialization of the model store to return a JSON representation of the model. Hood means calling serialize () on an instance of DS.Serializer. See DS.Model.serialize ()

I tried DS.Serializer.serialize (myrecord), but I got this error: doesn't have a 'serialize' method

Right DS.Serializer is a class and does not have a serialize method. Therefore DS.Serializer.serialize(myrecord) does not work. The serialize function you are associated with is an instance method, so it will be available in instances of the DS.Serializer class. However, DS.Serializer is an abstract base class, so it makes no sense to instantiate this object and try to call it serialize() .

For more details on how ember data serializers work, see [serializer api docs] ( https://github.com/emberjs/data/blob/761412849a56ad086c44659faafa547d8f6c03a8/packages/ember-data/lib/system/serializer.js#L10- L211 )

- UPDATED for an additional question: -

How can I directly call the serialization version of RESTAdapter? They made some changes to the general serializer.

When using RESTAdapter, you can simply call model.serialize() on the model. But in rare cases, when your models use something else (for example, FixtureAdapter), and you want to use the RESTSerializer serialization functions, you can create a serializer using DS.RESTSerializer.create({}) and then pass your model to the serialize() method serialize() . 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)); } }); 

The 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 an underscore. Example: here http://jsbin.com/uduzin/3/edit

+6
source share

All Articles