Skip to content

Instantly share code, notes, and snippets.

@knownasilya
Created August 2, 2013 14:09
Show Gist options
  • Save knownasilya/6140169 to your computer and use it in GitHub Desktop.
Save knownasilya/6140169 to your computer and use it in GitHub Desktop.
Ember Data modify props.
ajax: function (url, type, hash) {
var adapter = this;
return new Ember.RSVP.Promise(function (resolve, reject) {
hash = hash || {};
hash.url = url;
hash.type = type;
hash.dataType = "jsonp";
hash.context = adapter;
if (hash.data && type !== "GET") {
hash.contentType = "application/json; charset=utf-8";
hash.data = JSON.stringify(hash.data);
}
hash.success = function (json) {
var transformedJSON = adapter.transformJSON(json, hash.root);
Ember.run(null, resolve, transformedJSON);
};
hash.error = function (jqXHR, textStatus, errorThrown) {
Ember.run(null, reject, errorThrown);
};
jQuery.ajax(hash);
});
},
transformJSON: function (json, root) {
var feature,
index = 0,
pluralizedRoot = this.pluralize(root),
transformedJSON = {};
transformedJSON[pluralizedRoot] = [];
for(;index < json.features.length; index++) {
feature = json.features[index];
if(feature.geometry) {
feature.attributes.geometry = feature.geometry;
}
transformedJSON[pluralizedRoot].push(feature.attributes);
}
return transformedJSON;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment