Browse files

Add a camelize convenience to BasicAdapter

  • Loading branch information...
1 parent 25b78d0 commit 43668e2c7da52e80faa94ae42195b54e31e0f67a tomhuda committed Mar 7, 2013
View
14 packages/ember-data/lib/adapters/basic_adapter.js
@@ -1,3 +1,5 @@
+var camelize = Ember.String.camelize;
+
var ObjectProcessor = function(json, type, store) {
this.json = json;
this.type = type;
@@ -7,6 +9,18 @@ var ObjectProcessor = function(json, type, store) {
ObjectProcessor.prototype = {
load: function() {
this.store.load(this.type, this.json);
+ },
+
+ camelizeKeys: function() {
+ var json = this.json, value;
+
+ for (var prop in json) {
+ value = json[prop];
+ delete json[prop];
+ json[camelize(prop)] = value;
+ }
+
+ return this;
}
};
View
22 packages/ember-data/tests/integration/adapters/basic_adapter_test.js
@@ -43,3 +43,25 @@ test("The sync object is consulted to load data", function() {
equal(get(person, 'id'), "1", "The id is still the same");
}));
});
+
+test("A camelizeKeys() convenience will camelize all of the keys", function() {
+ Person.sync = {
+ find: function(id, process) {
+ setTimeout(async(function() {
+ process({ id: 1, first_name: "Tom", last_name: "Dale" })
+ .camelizeKeys()
+ .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, 'id'), "1", "The id is still the same");
+ }));
+});

0 comments on commit 43668e2

Please sign in to comment.