Skip to content
Browse files

Merge pull request #581 from Mochaleaf/enumTransform

Adding support for registering enumerated types in a more straightforward way
  • Loading branch information...
2 parents a70f609 + 72842a5 commit 471ef5a67c27ff68be14008d209ef5c5ab80e4dc @wycats wycats committed Jan 8, 2013
View
30 packages/ember-data/lib/system/adapter.js
@@ -540,6 +540,36 @@ DS.Adapter = Ember.Object.extend(DS._Mappable, {
},
/**
+ A public method that allows you to register an enumerated
+ type on your adapter. This is useful if you want to utilize
+ a text representation of an integer value.
+
+ Eg: Say you want to utilize "low","medium","high" text strings
+ in your app, but you want to persist those as 0,1,2 in your backend.
+ You would first register the transform on your adapter instance:
+
+ adapter.registerEnumTransform('priority', ['low', 'medium', 'high']);
+
+ You would then refer to the 'priority' DS.attr in your model:
+ App.Task = DS.Model.extend({
+ priority: DS.attr('priority')
+ });
+
+ And lastly, you would set/get the text representation on your model instance,
+ but the transformed result will be the index number of the type.
+
+ App: myTask.get('priority') => 'low'
+ Server Response / Load: { myTask: {priority: 0} }
+
+ @param {String} type of the transform
+ @param {Array} array of String objects to use for the enumerated values.
+ This is an ordered list and the index values will be used for the transform.
+ */
+ registerEnumTransform: function(attributeType, objects) {
+ get(this, 'serializer').registerEnumTransform(attributeType, objects);
+ },
+
+ /**
If the globally unique IDs for your records should be generated on the client,
implement the `generateIdForRecord()` method. This method will be invoked
each time you create a new record, and the value returned from it will be
View
13 packages/ember-data/lib/system/serializer.js
@@ -907,6 +907,19 @@ DS.Serializer = Ember.Object.extend({
this.transforms[type] = transform;
},
+ registerEnumTransform: function(type, objects) {
+ var transform = {
+ deserialize: function(deserialized) {
+ return objects.objectAt(deserialized);
+ },
+ serialize: function(serialized) {
+ return objects.indexOf(serialized);
+ },
+ values: objects
+ };
+ this.registerTransform(type, transform);
+ },
+
/**
MAPPING CONVENIENCE
*/
View
37 packages/ember-data/tests/integration/transform_test.js
@@ -116,3 +116,40 @@ test("the default date transform", function() {
equal(result2, true, "timestamp is transformed into a date");
equal(person2.get('born').toString(), date2.toString(), "date.toString and transformed date.toString values match");
});
+
+
+module("Enum Transforms", {
+ setup: function() {
+ adapter = DS.Adapter.create();
+ adapter.registerEnumTransform('materials', ['unobtainium', 'kindaobtainium', 'veryobtainium']);
+
+ store = DS.Store.create({
+ adapter: adapter
+ });
+
+ serializer = adapter.get('serializer');
+
+ Person = DS.Model.extend({
+ material: DS.attr('materials')
+ });
+ },
+ teardown: function() {
+ serializer.destroy();
+ adapter.destroy();
+ store.destroy();
+ }
+});
+
+test("correct transforms are applied", function() {
+ var json, person;
+ store.load(Person, {
+ id: 1,
+ material: 2
+ });
+
+ person = store.find(Person, 1);
+ equal(person.get('material'), 'veryobtainium', 'value of the attribute on the record should be transformed');
+
+ json = adapter.serialize(person);
+ equal(json.material, 2, 'value of the attribute in the JSON hash should be transformed');
+});

0 comments on commit 471ef5a

Please sign in to comment.
Something went wrong with that request. Please try again.