Last active
August 29, 2015 14:24
-
-
Save alex859/1bf0fd281c3e03b65bf7 to your computer and use it in GitHub Desktop.
Wrap a JSON object into an Ember object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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