I am working on an app using a Parse.com back end and an Angularjs front end. Parse uses Backbone-style objects with getters and setters, e.g. I can't just connect an object property to a text field with ng-model="object.property". The properties are available in the object.attributes property, but I believe that's not meant to be set to directly. For now, I'm doing something like (assuming keys is a list of the property names):
for (var i = 0; i < keys.length; i++) {
object[keys[i]] = object.get(keys[i]);
}
Then let things happen to the object in Angular, and then:
for (var i = 0; i < keys.length; i++) {
object.set(keys[i], object[keys[i]]);
}
object.save();
Can anyone think of a better way to handle this?
I wouldn't mess with the Model#set method because it involves internal stuff, but I can't think of anything wrong using model.attributes instead of Model#get if it'd be so much easier for you.
That said, I'd think you'd prefer those:
// Model#get
myModel.toJSON(); // returns a clone of model.attributes, cool eh?
// Model#set
myModel.set(JSON); // Model#get doesn't support it, but you can set several attributes at the same time
Those 2 methods do what you wanted, I guess.
You can remove ParseSDK and use this AngularJS module instead : https://github.com/jimrhoskins/angular-parse
There are a few things that are not ideal about the existing Parse JavaScript API in AngularJS. The existing API is modeled after Backbone Models and the main problem is setters are used instead of object properties. instance.set('property', 'value') doesn't really fit well with things like ng-model
Instead, angular-parse is based loosely on Spine Models where properties directly defined on the object are used. To facilitate this, when defining a model, it is "configured" by supplying the class name (as defined in Parse) as well as which properties are part of that class.
Angular-parse also uses promises for any methods making network calls.