0

This SO question explains how to store an array in an Ember model, but how could it be done with a custom object, which also isn't supported natively supported, according to the Guides.

This is an example of the object that I am building

obj[0] = {"timeInMinutes": ">120", "occurrences": 24 };
obj[1] = {"timeInMinutes": "90-120","occurrences": 69 };
obj[2] = {"timeInMinutes": "60-90", "occurrences":  53 };
obj[3] = {"timeInMinutes": "30-60", "occurrences": 35  };
obj[4] = {"timeInMinutes": "0-30", "occurrences": 24 };

Update.

Using the information provided in this answer, I was able to create an array attribute on my model along with several other values, but I also want to be able to create DS.attr('object') attribute to be used like this. To create an object type, do I need to use a DS.Transform.extend({ as was done with the array in the linked to SO answer?

 App.Index = DS.Model.extend({
    names: DS.attr('array'),
    country: DS.attr('string'),
    statistics: DS.attr('object')
Community
  • 1
  • 1
Leahcim
  • 40,649
  • 59
  • 195
  • 334

2 Answers2

0

If you’re using Ember Data, you’ll have to structure your models in an Ember Data-compatible way. You could have a parent Object (surely has a better name, but you didn’t explain your domain at all) that hasMany TimespanOccurrences or the like.

Buck Doyle
  • 6,333
  • 1
  • 22
  • 35
  • I'm sorry, this isn't clear to me. Maybe I didn't provide enough info. I updated the OP in the hopes that you could provide a bit more detail. Thanks in advance – Leahcim Jan 27 '15 at 00:52
0

If you want the property to be a primitive object and not an Ember Object then you can do the following:

ObjectTransform = DS.Transform.extend({
  deserialize: function(serialized) { return serialized; },
  serialize: function(deserialized) { return deserialized; }
});

This is assuming that you don't need to change the object at all after it comes in over the wire.

Kevin Bullaughey
  • 2,556
  • 25
  • 37
  • so how do I use it when setting up my model? like this? ```App.myModel = Ember.Model.extend({ name : DS.attr('string'), someObject : DS.attr('object') }``` – Leahcim Jan 28 '15 at 14:48
  • I have the exact same setup, but have used the naming `RawTransform` and `DS.attr('raw')`. I assume that using the word `object` will work too. Note that it's not just for objects. Anything that doesn't need serialization and deserialization can be used in this way, including arrays of primitives or arrays of objects. – Kevin Bullaughey Jan 28 '15 at 15:06