Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you serialise Ember objects?

I need to use localStorage to store some Ember objects. I notice that Ember objects have properties with names like __ember1334992182483. When I call JSON.stringify() on Ember objects, these __ember* properties are not serialised. Why is this? I'm not saying that I want to serialize those properties. I am just curious about what exactly they are and how they are implemented such that they are not serialised.

I am using cycle.js (https://github.com/douglascrockford/JSON-js/blob/master/cycle.js) to encode my data structures that contain duplicate references into a string that can be used for reconstructing the original data structures. It lets you do this:

a = {a:1}
b = {b:1}
c = [[a, b], [b, a]]

foo = JSON.stringify(JSON.decycle(c))  // "[[{'a':1},{'b':1}],[{'$ref':'$[0][1]'},{'$ref':'$[0][0]'}]]"
JSON.retrocycle(JSON.parse(foo))  // reconstruct c

For Ember objects I can do the same thing, but I also need to pass the deserialised objects into Ember.Object.create() because they are deserialised as plain JavaScript objects.

Is this the best way to serialise/deserialise Ember objects? Is there a recommended technique for this?

like image 843
hekevintran Avatar asked Apr 21 '12 07:04

hekevintran


People also ask

What is serializer in Ember?

In Ember Data a Serializer is used to serialize and deserialize records when they are transferred in and out of an external source. This process involves normalizing property names, transforming attribute values and serializing relationships. By default, Ember Data uses and recommends the JSONAPISerializer .

What is the default serializer shipped with Ember?

Ember Data ships with 3 serializers. The JSONAPISerializer is the default serializer and works with JSON:API backends.

What is a model in Ember?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.


1 Answers

For serialization and deserialization you could do something along this lines, see http://jsfiddle.net/pangratz666/NVpng/:

App.Serializable = Ember.Mixin.create({
    serialize: function() {
        var propertyNames = this.get('propertyNames') || [];
        return this.getProperties(propertyNames);
    },

    deserialize: function(hash) {
        this.setProperties(hash);
    }
});

App.Person = Ember.Object.extend(App.Serializable, {
    propertyNames: 'firstName title fullName'.w(),
    fullName: function() {
        return '%@ %@'.fmt(this.get('title'), this.get('firstName'));
    }.property('firstName', 'title')
});

var hansi = App.Person.create({
    firstName: 'Hansi',
    title: 'Mr.'
});

// { firstName: 'hansi', title: 'Mr.', fullName: 'Mr. Hansi' }
console.log( hansi.serialize() );

var hubert = App.Person.create();
hubert.deserialize({
    firstName: 'Hubert',
    title: 'Mr.'
});
console.log( hubert.serialize() );​

UPDATE: Also have a look at the similar question Ember model to json

like image 134
pangratz Avatar answered Sep 18 '22 14:09

pangratz