Merge pull request #1648 from apmorton/master

Add ember-data-django-rest-adapter 0.13.1
This commit is contained in:
Lachlan Collins
2013-08-06 19:15:54 -07:00
3 changed files with 307 additions and 1 deletions
@@ -0,0 +1,293 @@
// Version: 0.13.1
// Last commit: 198e16f (2013-08-01 10:09:55 -0400)
(function() {
var define, requireModule;
(function() {
var registry = {}, seen = {};
define = function(name, deps, callback) {
registry[name] = { deps: deps, callback: callback };
};
requireModule = function(name) {
if (seen[name]) { return seen[name]; }
seen[name] = {};
var mod, deps, callback, reified , exports;
mod = registry[name];
if (!mod) {
throw new Error("Module '" + name + "' not found.");
}
deps = mod.deps;
callback = mod.callback;
reified = [];
exports;
for (var i=0, l=deps.length; i<l; i++) {
if (deps[i] === 'exports') {
reified.push(exports = {});
} else {
reified.push(requireModule(deps[i]));
}
}
var value = callback.apply(this, reified);
return seen[name] = exports || value;
};
})();
(function() {
DS.DjangoRESTSerializer = DS.RESTSerializer.extend({
patchInJSONRoot: function(json, type, many) {
var pJSON, root;
pJSON = {};
root = this.rootForType(type);
if (many === true) {
root = this.pluralize(root);
}
pJSON[root] = json;
return pJSON;
},
keyForHasMany: function(type, name) {
return this.keyForAttributeName(type, name);
},
keyForBelongsTo: function(type, name) {
return this.keyForAttributeName(type, name);
},
extract: function(loader, json, type, records) {
json = this.patchInJSONRoot(json, type, false);
this._super(loader, json, type, records);
},
extractMany: function(loader, json, type, records) {
json = this.patchInJSONRoot(json, type, true);
this._super(loader, json, type, records);
},
// modified version of https://github.com/emberjs/data/blob/master/packages/ember-data/lib/serializers/json_serializer.js#L169
// Django Rest Framework expects a non-embedded has-many to serialize to an
// array of ids, but the JSONSeriarlizer assumed non-embedded relationship
// updates would happen in the related model.
addHasMany: function(hash, record, key, relationship) {
var type = record.constructor,
name = relationship.key,
serializedHasMany = [],
includeType = (relationship.options && relationship.options.polymorphic),
manyArray, embeddedType;
// Get the DS.ManyArray for the relationship off the record
// manyArray = get(record, name);
manyArray = record.get(name);
// If the has-many is not embedded, send just the array of ids.
embeddedType = this.embeddedType(type, name);
if (embeddedType !== 'always') {
// Build up the array of ids
manyArray.forEach(function (record) {
serializedHasMany.push(this.serializeId(record.id));
}, this);
} else {
// Build up the array of serialized records
manyArray.forEach(function (record) {
serializedHasMany.push(this.serialize(record, { includeId: true, includeType: includeType }));
}, this);
}
// Set the appropriate property of the serialized JSON to the
// array of serialized embedded records or array of ids
hash[key] = serializedHasMany;
}
});
})();
(function() {
function rejectionHandler(reason) {
Ember.Logger.error(reason, reason.message);
throw reason;
}
DS.DjangoRESTAdapter = DS.RESTAdapter.extend({
bulkCommit: false,
serializer: DS.DjangoRESTSerializer,
createRecord: function(store, type, record) {
var root, adapter, data;
root = this.rootForType(type);
adapter = this;
data = this.serialize(record);
return this.ajax(this.buildURL(root), "POST", {
data: data
}).then(function(json) {
adapter.didCreateRecord(store, type, record, json);
}, function(xhr) {
adapter.didError(store, type, record, xhr);
throw xhr;
}).then(null, rejectionHandler);
},
updateRecord: function(store, type, record) {
var id, root, adapter, data;
id = Ember.get(record, 'id');
root = this.rootForType(type);
adapter = this;
data = this.serialize(record);
return this.ajax(this.buildURL(root, id), "PUT", {
data: data
}).then(function(json) {
adapter.didUpdateRecord(store, type, record, json);
}, function(xhr) {
adapter.didError(store, type, record, xhr);
throw xhr;
}).then(null, rejectionHandler);
},
findMany: function(store, type, ids, parent) {
var adapter, root, url;
adapter = this;
if (parent) {
url = this.buildFindManyUrlWithParent(type, parent);
} else {
root = this.rootForType(type);
url = this.buildURL(root);
}
return this.ajax(url, "GET", {
}).then(function(json) {
adapter.didFindMany(store, type, json);
}).then(null, rejectionHandler);
},
ajax: function(url, type, hash) {
hash = hash || {};
hash.cache = false;
return this._super(url, type, hash);
},
buildURL: function(record, suffix) {
var url = this._super(record, suffix);
if (url.charAt(url.length -1) !== '/') {
url += '/';
}
return url;
},
buildFindManyUrlWithParent: function(type, parent) {
var root, url, endpoint, parentType, parentValue;
endpoint = parent.get('findManyKey');
parentType = parent.get('findManyType');
if (typeof endpoint !== 'string') {
parent.eachRelationship(function(name, relationship) {
if (relationship.kind === 'hasMany' && relationship.type === type) {
endpoint = relationship.key;
parentType = relationship.parentType;
}
});
}
Ember.assert("could not find a relationship for the specified child type", typeof endpoint !== "undefined");
endpoint = this.serializer.keyForAttributeName(parentType, endpoint);
parentValue = parent.get('id');
root = this.rootForType(parentType);
url = this.buildURL(root, parentValue);
return url + endpoint + '/';
},
/**
RESTAdapter expects HTTP 422 for invalid records and a JSON response
with errors inside JSON root `errors`, however DRF uses 400
and errors without a JSON root.
*/
didError: function(store, type, record, xhr) {
if (xhr.status === 400) {
var data = JSON.parse(xhr.responseText);
var errors = {};
// Convert error key names
record.eachAttribute(function(name) {
var attr = this.serializer.keyForAttributeName(type, name);
if (attr in data) {
errors[name] = data[attr];
}
}, this);
record.eachRelationship(function(name, relationship) {
var attr;
if (relationship.kind === 'belongsTo') {
attr = this.serializer.keyForBelongsTo(type, name);
} else {
attr = this.serializer.keyForHasMany(type, name);
}
if (attr in data) {
errors[name] = data[attr];
}
}, this);
store.recordWasInvalid(record, errors);
} else {
this._super(store, type, record, xhr);
}
}
});
})();
(function() {
DS.DjangoRESTStore = DS.Store.extend({
findMany: function(type, idsOrReferencesOrOpaque, record, relationship) {
var ret;
// check for hasMany relationship
if (typeof relationship === 'object' && relationship.kind === 'hasMany') {
record.set('findManyKey', relationship.key);
record.set('findManyType', relationship.parentType);
}
ret = this._super(type, idsOrReferencesOrOpaque, record, relationship);
// clear the variables we set to be clean
record.set('findManyKey', null);
record.set('findManyType', null);
return ret;
}
});
})();
(function() {
})();
})();
@@ -0,0 +1,13 @@
// ==========================================================================
// Project: Ember Data Django Rest Adapter
// Copyright: (c) 2013 Toran Billups http://toranbillups.com
// License: MIT
// ==========================================================================
// Version: 0.13.1
// Last commit: 198e16f (2013-08-01 10:09:55 -0400)
!function(){var t,e;!function(){var n={},i={};t=function(t,e,i){n[t]={deps:e,callback:i}},e=function(t){if(i[t])return i[t];i[t]={};var r,o,a,s,u;if(r=n[t],!r)throw new Error("Module '"+t+"' not found.");o=r.deps,a=r.callback,s=[];for(var h=0,d=o.length;d>h;h++)"exports"===o[h]?s.push(u={}):s.push(e(o[h]));var c=a.apply(this,s);return i[t]=u||c}}(),function(){DS.DjangoRESTSerializer=DS.RESTSerializer.extend({patchInJSONRoot:function(t,e,n){var i,r;return i={},r=this.rootForType(e),n===!0&&(r=this.pluralize(r)),i[r]=t,i},keyForHasMany:function(t,e){return this.keyForAttributeName(t,e)},keyForBelongsTo:function(t,e){return this.keyForAttributeName(t,e)},extract:function(t,e,n,i){e=this.patchInJSONRoot(e,n,!1),this._super(t,e,n,i)},extractMany:function(t,e,n,i){e=this.patchInJSONRoot(e,n,!0),this._super(t,e,n,i)},addHasMany:function(t,e,n,i){var r,o,a=e.constructor,s=i.key,u=[],h=i.options&&i.options.polymorphic;r=e.get(s),o=this.embeddedType(a,s),"always"!==o?r.forEach(function(t){u.push(this.serializeId(t.id))},this):r.forEach(function(t){u.push(this.serialize(t,{includeId:!0,includeType:h}))},this),t[n]=u}})}(),function(){function t(t){throw Ember.Logger.error(t,t.message),t}DS.DjangoRESTAdapter=DS.RESTAdapter.extend({bulkCommit:!1,serializer:DS.DjangoRESTSerializer,createRecord:function(e,n,i){var r,o,a;return r=this.rootForType(n),o=this,a=this.serialize(i),this.ajax(this.buildURL(r),"POST",{data:a}).then(function(t){o.didCreateRecord(e,n,i,t)},function(t){throw o.didError(e,n,i,t),t}).then(null,t)},updateRecord:function(e,n,i){var r,o,a,s;return r=Ember.get(i,"id"),o=this.rootForType(n),a=this,s=this.serialize(i),this.ajax(this.buildURL(o,r),"PUT",{data:s}).then(function(t){a.didUpdateRecord(e,n,i,t)},function(t){throw a.didError(e,n,i,t),t}).then(null,t)},findMany:function(e,n,i,r){var o,a,s;return o=this,r?s=this.buildFindManyUrlWithParent(n,r):(a=this.rootForType(n),s=this.buildURL(a)),this.ajax(s,"GET",{}).then(function(t){o.didFindMany(e,n,t)}).then(null,t)},ajax:function(t,e,n){return n=n||{},n.cache=!1,this._super(t,e,n)},buildURL:function(t,e){var n=this._super(t,e);return"/"!==n.charAt(n.length-1)&&(n+="/"),n},buildFindManyUrlWithParent:function(t,e){var n,i,r,o,a;return r=e.get("findManyKey"),o=e.get("findManyType"),"string"!=typeof r&&e.eachRelationship(function(e,n){"hasMany"===n.kind&&n.type===t&&(r=n.key,o=n.parentType)}),r=this.serializer.keyForAttributeName(o,r),a=e.get("id"),n=this.rootForType(o),i=this.buildURL(n,a),i+r+"/"},didError:function(t,e,n,i){if(400===i.status){var r=JSON.parse(i.responseText),o={};n.eachAttribute(function(t){var n=this.serializer.keyForAttributeName(e,t);n in r&&(o[t]=r[n])},this),n.eachRelationship(function(t,n){var i;i="belongsTo"===n.kind?this.serializer.keyForBelongsTo(e,t):this.serializer.keyForHasMany(e,t),i in r&&(o[t]=r[i])},this),t.recordWasInvalid(n,o)}else this._super(t,e,n,i)}})}(),function(){DS.DjangoRESTStore=DS.Store.extend({findMany:function(t,e,n,i){var r;return"object"==typeof i&&"hasMany"===i.kind&&(n.set("findManyKey",i.key),n.set("findManyType",i.parentType)),r=this._super(t,e,n,i),n.set("findManyKey",null),n.set("findManyType",null),r}})}()}(),"undefined"==typeof location||"localhost"!==location.hostname&&"127.0.0.1"!==location.hostname||Ember.Logger.warn("You are running a production build of Ember on localhost and won't receive detailed error messages. If you want full error messages please use the non-minified build provided on the Ember website.");
@@ -1,7 +1,7 @@
{
"name": "ember-data-django-rest-adapter",
"filename": "ember-data-django-rest-adapter.min.js",
"version": "0.13",
"version": "0.13.1",
"description": "An ember-data adapter for django web applications powered by the django-rest-framework",
"homepage": "https://github.com/toranb/ember-data-django-rest-adapter",
"keywords": [