Skip to content

Loading…

Adding support for registering enumerated types in a more straightforward way #581

Merged
merged 3 commits into from

3 participants

@leepfrog

Original PR #505

@leepfrog

@tomdale included docs and passing test :hamster:

@ahawkins

@leepfrog has there been any movement on this? This looks pretty good.

@wycats wycats merged commit 471ef5a into emberjs:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 4, 2013
  1. @leepfrog
Commits on Jan 6, 2013
  1. @leepfrog

    Adding in a values property to the transform so it's easy to obtain t…

    leepfrog committed
    …he list of enumerated values for a transform
  2. @leepfrog
This page is out of date. Refresh to see the latest.
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');
+});
Something went wrong with that request. Please try again.