Skip to content

Instantly share code, notes, and snippets.

@csprocket777
Created May 19, 2014 21:48
Show Gist options
  • Save csprocket777/456fac14f21d7af32d74 to your computer and use it in GitHub Desktop.
Save csprocket777/456fac14f21d7af32d74 to your computer and use it in GitHub Desktop.
Ember-Data Poop box
var adapter = DS.RESTAdapter.extend({
host: window.apiHost,
ajax: function(url, method, hash){
hash = hash || {};
hash.crossDomain = true;
return this._super(url, method, hash);
},
findQuery: function(store, type, id) {
var allOfType = store.all(type);
if( allOfType.content.length === 0 )
{
return this._super.apply(this, arguments);
}else{
return new Ember.RSVP.Promise(function(resolve, reject){
allOfType = allOfType.filter(function(item){
var $return = true;
for(var prop in id){
if( $return === false )
{
return $return;
}else{
switch( Ember.get(type, 'fields').get(prop) )
{
case "attribute":
$return = item.get(prop) === id[prop];
break;
case "belongsTo":
$return = item.get(prop + '.id') === id[prop];
break;
case "hasMany":
$return = item.get(prop + '.id') === id[prop];
break;
}
}
}
return $return;
}.bind(this));
allOfType = allOfType.map(function(item){ return item.serialize({includeId: true}); });
var $returnObj = {};
$returnObj[type.typeKey.pluralize()] = allOfType;
resolve($returnObj);
}.bind('this'));
}
},
findMany: function(store, type, ids) {
var allOfType = store.all(type);
if( allOfType.content.length === 0 )
{
return this._super.apply(this, arguments);
}else{
return new Ember.RSVP.Promise(function(resolve, reject){
allOfType = allOfType.filter(function(item){
return ids.contains(item.get("id"));
}.bind(this));
allOfType = allOfType.map(function(item){ return item.serialize({includeId: true}); });
var $returnObj = {};
$returnObj[type.typeKey.pluralize()] = allOfType;
resolve($returnObj);
}.bind('this'));
}
}
});
export default adapter;
@csprocket777
Copy link
Author

Ok. this is currently MORE poop. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment