How to declare an Array in the model?


#1

I tried using DS.attr(‘array’), but I get the error: Uncaught Error: Assertion Failed: Unable to find transform for ‘array’

I’m trying to pushObject several pieces of data into this array.

Thanks.


#2

You’ll need to make a custom Transform for DS.attr('array'). If you’re using ember-cli, then ember g transform array and use this:

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

export default DS.Transform.extend({
	deserialize: function(serialized) {
		if (Ember.isArray(serialized)) {
			return Ember.A(serialized);
		} else {
			return Ember.A();
		}
	},

	serialize: function(deserialized) {
		if (Ember.isArray(deserialized)) {
			return Ember.A(deserialized);
		} else {
			return Ember.A();
		}
	}
});

Another, slightly more flexible transform is the answer to this Stack Overflow question, but you get the idea. http://stackoverflow.com/questions/12168570/how-to-represent-arrays-within-ember-data-models