Skip to content

Instantly share code, notes, and snippets.

@alex859
Last active August 29, 2015 14:24
Show Gist options
  • Save alex859/1bf0fd281c3e03b65bf7 to your computer and use it in GitHub Desktop.
Save alex859/1bf0fd281c3e03b65bf7 to your computer and use it in GitHub Desktop.
Wrap a JSON object into an Ember object
App.fromJSON = function (json)
{
var result = undefined;
if (Ember.typeOf(json) === 'array')
{
result = Ember.A();
json.forEach(function (el){
result.pushObject(App.fromJSON(el));
});
}
else if (Ember.typeOf(json) === 'object')
{
result = Ember.Object.create();
for (var key in json)
{
if (json.hasOwnProperty(key))
{
result.set(key, App.fromJSON(json[key]));
}
}
}
else if (Ember.typeOf(json) === 'string' || Ember.typeOf(json) === 'number' || Ember.typeOf(json) === 'boolean')
{
result = json;
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment