vendredi 24 avril 2015

handling models with embedded objects in the payloads with ember data

I'm trying to determine how best to define my models. I'm trying to represent an asset. Ex: car, tv, house, etc.

Here's the payload coming from my server:

{
    "asset": {
        "id": "1b0b77a2-28d7-488e-9a6f-8a202c297593",
        "created_at": "2015-03-19T09:16:43+0500",
        "updated_at": "2015-03-19T09:16:43+0500",
        "name": "Flat Screen Tv",
        "properties": [
            {
                "name": "brand",
                "value": "Sony",
                "public": false,
                "family": false,
                "friends": false
            },
            {
                "name": "price",
                "value": 1000,
                "public": false,
                "family": false,
                "friends": false
            }
        ]
    }
}

It needs to get POSTed back in the same format. How can I define this model with ember data? The properties are hanging me up. I thought I could just define a transformer but then noticed that changing values in the transformed data doesn't update the asset's "dirty" flag. I need that dirty flag to be updated when the properties get changed. Any ideas?

// app/transforms/array.js
import Ember from 'ember';
import DS from 'ember-data';

export default DS.Transform.extend({
  serialize: function(value) {
    return value.toArray();
  },
  deserialize: function(value) {
    return Ember.A(value);
  }
});

Here's my asset model definition.

import DS from 'ember-data';

// app/models/asset.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  createdAt: DS.attr('date'),
  updatedAt: DS.attr('date'),
  properties: DS.attr('array')
});




Aucun commentaire:

Enregistrer un commentaire