Created
April 12, 2014 04:56
-
-
Save jakecraige/10519421 to your computer and use it in GitHub Desktop.
Ember.js Parse.com js wrapper. A little class I wrote to wrap the actions of the Parse.com framework to behave like ember-data, without ember data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toEmberObject(result) { | |
var object = ParseModel.create(result.attributes); | |
object._parseAttributes = Ember.keys(result.attributes) | |
object._parseObject = result; | |
object.set('id', result.id) | |
object.set('createdAt', result.createdAt) | |
return object; | |
} | |
function mapToEmberObjects(results) { | |
return results.map(function(result) { | |
return toEmberObject(result); | |
}); | |
} | |
function wrapParseModelMethod(method, self) { | |
return new Ember.RSVP.Promise(function(resolve, reject){ | |
self._parseObject[method]({ | |
success: function(result) { | |
resolve(toEmberObject(result)); | |
}, | |
error: function(error) { | |
reject(error); | |
} | |
}); | |
}); | |
} | |
// wrap collection method that returns a single object | |
function wrapParseCollectionObjectMethod(query, method) { | |
var args = [].slice.call(arguments), | |
params = [] | |
if(args.length > 2) { | |
params = args.slice(2, args.length); | |
} | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
params.push({ | |
success: function(result) { | |
resolve(toEmberObject(result)); | |
}, | |
error: function(error) { | |
reject(error); | |
} | |
}); | |
query[method].apply(query, params) | |
}); | |
} | |
// wrap collection method that returns an array of objects | |
function wrapParseCollectionArrayMethod(query, method) { | |
var args = [].slice.call(arguments), | |
params = [] | |
if(args.length > 2) { | |
params = args.slice(2, args.length); | |
} | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
params.push({ | |
success: function(results) { | |
resolve(mapToEmberObjects(results)); | |
}, | |
error: function(error) { | |
reject(error); | |
} | |
}); | |
query[method].apply(query, params) | |
}); | |
} | |
var ParseModel = Ember.Object.extend({ | |
increment: function(property){ | |
this._parseObject.increment(property); | |
this._super(property): | |
}, | |
// when calling set, update the parseObject with the attribute and update the | |
// object itself | |
set: function(key, value){ | |
this._parseObject.set(key, value); | |
this._super(key, value); | |
}, | |
save: function() { | |
// we go through the attributes and manually update them since the automatic | |
// view binding isn't updating the parseObject attributes automatically | |
this._parseAttributes.forEach(function(key) { | |
this._parseObject.set(key, this.get(key)) | |
}, this); | |
return wrapParseModelMethod("save", this); | |
}, | |
destroy: function() { | |
return wrapParseModelMethod("destroy", this); | |
}, | |
add: Ember.K, | |
addUnique: Ember.K, | |
remove: Ember.K, | |
}); | |
var ParseCollection = Ember.Object.extend({ | |
init: function() { | |
if(this.get('klassName') == null) throw new Error('Must set a klassName to parse-wrapper'); | |
this.set('klassObject', Parse.Object.extend(this.get('klassName'))); | |
}, | |
first: function() { | |
return wrapParseCollectionObjectMethod(this.newQuery(), 'first'); | |
}, | |
// pass in a object with keys as the method and val as the params, if there | |
// are more than one it should be an array | |
// | |
// ex: Bar.findQuery({descending: 'name', startsWith: ['name', 'Kung']}); | |
findQuery: function(queries){ | |
var query = this.newQuery() | |
Ember.keys(queries).forEach(function(key){ | |
if(Ember.isArray(queries[key])) { | |
query[key].apply(query, queries[key]) | |
} else { | |
query[key].call(query, queries[key]) | |
} | |
}); | |
return wrapParseCollectionArrayMethod(query, 'find'); | |
}, | |
newQuery: function() { | |
return new Parse.Query(this.get('klassObject')); | |
}, | |
findAll: function() { | |
return wrapParseCollectionArrayMethod(this.newQuery(), 'find'); | |
}, | |
find: function(id) { | |
if(id == null) { | |
return this.findAll(); | |
} | |
return wrapParseCollectionObjectMethod(this.newQuery(), 'get', id); | |
}, | |
}) | |
export default ParseCollection; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment