Skip to content

Instantly share code, notes, and snippets.

@taras

taras/deployd.js Secret

Created August 4, 2013 14:32
Show Gist options
  • Save taras/87c3c112ed5a2b17d791 to your computer and use it in GitHub Desktop.
Save taras/87c3c112ed5a2b17d791 to your computer and use it in GitHub Desktop.
var get = Ember.get, set = Ember.set;
Deployd = Ember.Namespace.create({});
Deployd.RestAdapter = Ep.RestAdapter.extend({
didReceiveDataForFind: function( data, type ) {
return this._super( this._wrapData( data, type ), type );
},
didReceiveDataForLoad: function( data, type, id ) {
return this._super( this._wrapData( data, type ), type, id );
},
_wrapData: function ( data, type ) {
var plural = this.pluralize( this.rootForType( type ) );
var wrapped = {};
wrapped[ plural ] = data;
return wrapped;
},
/**
* Update & Create should be fixed by adding a callback to EPF RestAdapter
* @param model
* @returns {Ember.RSVP.Promise.then|*|Ep.LazyModel.then|*|Promise|makePromise.then|*|makePromise.then|*|Promise.then|*|r.then|*|r.then|*|r.then|*|h.then|*|Ep.LazyModel.then|*|Ep.LazyModel.then|*|Ep.LazyModel.then|*|Ep.LazyModel.then|*|r.then|*|Ember.DeferredMixin.then|*|then|*|jQuery.Deferred.then|*|jQuery.Deferred.then|*|jQuery.Deferred.then|*|jQuery.Deferred.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|promise.then|*|Promise.then|*|Deferred.then|*|Promise.then|*|Deferred.then|*|d.then|*|c.then|*|Ember.DeferredMixin.then|*|then|*|Promise.then|*|Deferred.then|*|Transition.then|*|jQuery.Deferred.then|*|thenable.then|*|obj.then|*}
*/
update: function (model) {
var id, root, adapter, data, type = get(model, 'type');
id = get(model, 'id');
root = this.rootForType(type);
adapter = this;
data = get(this, 'serializer').serialize(model);
return this.ajax(this.buildURL(root, id), 'PUT', { data: data }).then(function (json) {
return Ember.run(adapter, 'didReceiveData', json, model);
}, function (xhr) {
throw Ember.run(adapter, 'didError', xhr, model);
});
},
create: function (model) {
var type = get(model, 'type');
var root = this.rootForType(type);
var adapter = this;
var data = get(this, 'serializer').serialize(model, { includeId: true });
return this.ajax(this.buildURL(root), 'POST', { data: data }).then(function (json) {
return Ember.run(adapter, 'didReceiveData', json, model);
}, function (xhr) {
throw Ember.run(adapter, 'didError', xhr, model);
});
},
didReceiveData: function (data, targetModel) {
var result = null;
var type = get( targetModel, 'type' );
var wrapped = {};
wrapped = this._wrapData( [ data ], type );
this.processData(wrapped, function (model) {
if (targetModel && model.isEqual(targetModel)) {
result = model;
}
});
return result;
}
});
Deployd.RestSerializer = Ep.RestSerializer.extend({
/**
* Deployd accepts camelized attributes names, so were not going to change the names
* @param type
* @param name
* @returns string
*/
keyForAttributeName: function (type, name) {
return name;
},
clientKey: function (type) {
return 'clientId';
},
clientRevision: function (type) {
return 'clientRev';
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment