Permalink
Browse files

Provide a transform mechanism for BasicAdapter

  • Loading branch information...
1 parent 43668e2 commit 02302abed6a8c531669a9ce4db2e6927a9658b46 tomhuda committed Mar 7, 2013
@@ -1,4 +1,17 @@
-var camelize = Ember.String.camelize;
+var camelize = Ember.String.camelize,
+ get = Ember.get,
+ registeredTransforms;
+
+var passthruTransform = {
+ serialize: function(value) { return value; },
+ deserialize: function(value) { return value; }
+};
+
+var defaultTransforms = {
+ string: passthruTransform,
+ boolean: passthruTransform,
+ number: passthruTransform
+};
var ObjectProcessor = function(json, type, store) {
this.json = json;
@@ -8,7 +21,7 @@ var ObjectProcessor = function(json, type, store) {
ObjectProcessor.prototype = {
load: function() {
- this.store.load(this.type, this.json);
+ this.store.load(this.type, {}, this.json);
},
camelizeKeys: function() {
@@ -20,6 +33,26 @@ ObjectProcessor.prototype = {
json[camelize(prop)] = value;
}
+ return this;
+ },
+
+ applyTransforms: function(transformType) {
+ var transforms = registeredTransforms[transformType],
+ json = this.json;
+
+ Ember.assert("You are trying to apply the '" + transformType + "' transforms, but you didn't register any transforms with that name", transforms);
+
+ get(this.type, 'attributes').forEach(function(name, attribute) {
+ var attributeType = attribute.type,
+ value = json[name];
+
+ var transform = transforms[attributeType] || defaultTransforms[attributeType];
+
+ Ember.assert("Your model specified the '" + attributeType + "' type for the '" + name + "' attribute, but no transform for that type was registered", transform);
+
+ json[name] = transform.deserialize(value);
+ });
+
return this;
}
};
@@ -41,3 +74,13 @@ DS.BasicAdapter = DS.Adapter.extend({
sync.find(id, processorFactory(store, type));
}
});
+
+DS.registerTransforms = function(kind, object) {
+ registeredTransforms[kind] = object;
+};
+
+DS.clearTransforms = function() {
+ registeredTransforms = {};
+};
+
+DS.clearTransforms();
@@ -11,12 +11,26 @@ module("Basic Adapter", {
var attr = DS.attr;
Person = DS.Model.extend({
firstName: attr('string'),
- lastName: attr('string')
+ lastName: attr('string'),
+ createdAt: attr('date')
+ });
+
+ DS.registerTransforms('test', {
+ date: {
+ serialize: function(value) {
+ return value.toString();
+ },
+
+ deserialize: function(string) {
+ return new Date(string);
+ }
+ }
});
},
teardown: function() {
Ember.run(function() {
+ DS.clearTransforms();
store.destroy();
adapter.destroy();
});
@@ -65,3 +79,26 @@ test("A camelizeKeys() convenience will camelize all of the keys", function() {
equal(get(person, 'id'), "1", "The id is still the same");
}));
});
+
+test("An applyTransforms method will apply registered transforms", function() {
+ Person.sync = {
+ find: function(id, process) {
+ setTimeout(async(function() {
+ process({ id: 1, firstName: "Tom", lastName: "Dale", createdAt: "1986-06-09" })
+ .applyTransforms('test')
+ .load();
+ }));
+ }
+ };
+
+ var person = Person.find(1);
+
+ equal(get(person, 'id'), "1", "The id is the coerced ID passed to find");
+
+ person.on('didLoad', async(function() {
+ equal(get(person, 'firstName'), "Tom");
+ equal(get(person, 'lastName'), "Dale");
+ equal(get(person, 'createdAt').valueOf(), new Date("1986-06-09").valueOf(), "The date was properly transformed");
+ equal(get(person, 'id'), "1", "The id is still the same");
+ }));
+});

0 comments on commit 02302ab

Please sign in to comment.