Skip to content

Instantly share code, notes, and snippets.

@ppcano
Created September 26, 2012 10:55
Show Gist options
  • Select an option

  • Save ppcano/3787318 to your computer and use it in GitHub Desktop.

Select an option

Save ppcano/3787318 to your computer and use it in GitHub Desktop.
Adapter
Yn.DjangoTastypieAdapter = DS.DjangoTastypieAdapter.extend({
/*
* API credential to be inserted in the ajax requests
*/
authHeader: null,
_urlifyData: function(type, model, raw){
var self = this;
var value;
var jsonData = model.toJSON({ associations: true });
var associations = Em.get(type, 'associationsByName');
associations.forEach(function(key, meta){
if (meta.kind === "belongsTo") {
key = meta.options.key || Em.get(model, 'namingConvention').foreignKey(key);
} else if (meta.kind === "hasMany") {
key = meta.options.key || Em.get(model, 'namingConvention').keyToJSONKey(key);
}
if ( meta.options.cache ) {
delete jsonData[key];
} else {
if (meta.kind === "belongsTo") {
value = jsonData[key];
if (!!value) {
jsonData[key] = self.getItemUrl(type, meta, value);
}
} else if (meta.kind === "hasMany") {
value = jsonData[key] || [];
$.each(value, function(i, item) {
value[i] = self.getItemUrl(type, meta, item);
});
}
}
});
return (raw) ? jsonData : JSON.stringify(jsonData);
},
/*
* Create a record in the Django server. POST actions must
* be enabled in the Resource
*/
createRecord: function(store, type, model) {
var self = this;
var root = this.rootForType(type);
var data = this._urlifyData(type, model);
this.ajax(root, "POST", {
data: data,
success: function(json) {
json = self._deurlifyData(type, json);
store.didCreateRecord(model, json);
},
error: function(error) {
model.fire('didCreateError', error);
}
});
},
/*
* Edit a record in the Django server. PUT actions must
* be enabled in the Resource
*/
updateRecord: function(store, type, model) {
var self = this;
var id = get(model, 'id');
var root = this.rootForType(type);
var data = this._urlifyData(type, model);
var url = [root, id].join("/");
this.ajax(url, "PUT", {
data: data,
success: function(json) {
json = self._deurlifyData(type, json);
store.didUpdateRecord(model, json);
},
error: function(error) {
model.fire('didUpdateError', error);
}
});
},
deleteRecord: function(store, type, model) {
var id = Em.get(model, 'id'),
root = this.rootForType(type),
url = [root, id].join("/");
this.ajax(url, "DELETE", {
success: function(json) {
store.didDeleteRecord(model);
},
error: function(error) {
model.fire('didDeleteError', error);
}
});
},
findAll: function(store, type) {
Ember.assert('Yoin findAll is not allowed, use find({})');
},
find: function(store, type, id, query) {
// OJO: this could break ember-data API
if (!query) query = {};
// FindMany array through subset of resources
if (id instanceof Array) {
var result = '';
for (var i = 0; i < id.length; i++) {
// can change city test
if (id[i] instanceof Object ) {
result+= id[i].id+';';
} else {
result+= id[i]+';';
}
}
id = "set/" + result.slice(0, -1);
//id = "set/" + id.join(";");
}
var root = this.rootForType(type),
url = [root, id].join("/"),
self = this;
this.ajax(url, "GET", {
data: query,
success: function(json) {
// Loads collection for findMany
if (json.hasOwnProperty("objects")) {
json["objects"].forEach(function(item, i, collection) {
collection[i] = self._deurlifyData(type, item);
});
store.loadMany(type, json["objects"]);
// Loads unique element with find by id
} else {
json = self._deurlifyData(type, json);
store.load(type, json);
}
},
error: function(xhr) {
// set automatically isError state
store.load(type, id, {});
}
});
},
findQuery: function(store, type, query, recordArray){
var self = this,
root = this.rootForType(type);
this.ajax(root, "GET", {
data: query,
success: function(json) {
json["objects"].forEach(function(item, i, collection) {
collection[i] = self._deurlifyData(type, item);
});
recordArray.load(json["objects"]);
},
error: function(xhr) {
recordArray.set('isError', true);
}
});
},
ajax: function(url, type, hash) {
hash.cache = false;
// If there is a locale defined, send the Accept-Language header
if (typeof I18n !== "undefined" && !!I18n.defaultLocale) {
hash.headers = jQuery.extend(hash.headers, {"Accept-Language": I18n.defaultLocale});
}
// If the user is logged in. Send his credentials
if (!!this.get('authHeader')) {
hash.headers = jQuery.extend(hash.headers, {
"Authorization": this.get('authHeader')
});
}
this._super(url, type, hash);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment