3

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.

HaoQi Li
  • 11,970
  • 14
  • 58
  • 77

2 Answers2

7

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

Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
  • `myrecord.serialize()` works! Thanks. Really dumb question, how come `serialize(myrecord)` wouldn't work? – HaoQi Li Jul 09 '13 at 22:32
  • I have added a related question. – HaoQi Li Jul 10 '13 at 00:35
  • 2
    `serialize(myrecord)` doesn't work because the `DS.Serializer` __class__ does not have a serialize method. So when you call `DS.Serializer.serialize(myrecord)` you get undefined method error. Same as if you called `DS.Serializer.randomMethodName()`. The serialize method is only available on __instances__ of DS.Serializer. So for example `var serializer = DS.FixtureSerializer.create({}); serializer.serialize(myrecord);` will work. – Mike Grassotti Jul 10 '13 at 02:53
  • 1
    Thanks Mike. Just summarizing the answer for the additional question: var serializer = DS.RESTSerializer.create({}); serializer.serialize(record);' – HaoQi Li Jul 10 '13 at 03:24
0

This answer might be a bit more up-to-date.

If your model name is "post" it would be:

const serializer = this.get('store').serializerFor('post');

which gives you access to all your serializer functions.

serializer.serialize(myRecord);

Ember will complain if you don't have an associated model for a serializer.

Rimian
  • 36,864
  • 16
  • 117
  • 117