Based on the brilliant Parse Angular Patch. Adjusted to make more Angularic.
- Dependency Injection of the Parse SDK as "ngParse"
- All Parse async functions return Angular promises ($q) instead of Parse.Promise
- Parse.Object and Parse.User can be used in an angular way (i.e. straight property access no, (g|s)etters)
# from the terminal at the root of your project
bower install angular-parse-patch --save
angular.module('xxxx', ['mcwebb.parse-patch'])
angular.module('xxxx')
.config(function (ngParseProvider) {
ngParseProvider.initialize(
'YOUR_API_ID',
'YOUR_JS_KEY'
);
});
When creating a Parse object you must define the fields you want to be able to access without getters/setters.
angular.module('xxxx')
.controller('myController', function (ngParse, $scope) {
var Cat = ngParse.Object.extend('Cat', {
fields: [
'name',
'colour',
'breed'
]
});
$scope.cat = new Cat();
});
Adding instance methods (like purr below) is of course completly optional. However if you need to define some initialization logic, be sure to call the parent method, otherwise you won't get all the Angularic field goodness.
angular.module('xxxx')
.controller('myController', function (ngParse, $scope) {
var Cat = ngParse.Object.extend('Cat', {
fields: [
'name',
'colour',
'breed'
],
initialize: function () {
// call the parent to bind those fields
ngParse.Object.prototype.initialize.apply(this, arguments);
// custom logic
if (typeof this.breed == 'undefined')
this.breed = [];
},
purr: function () {
console.log(this.name + ' is happy!');
}
});
$scope.cat = new Cat();
});
<input type="text" ng-model="cat.name" />
<button ng-click="cat.purr()">Make me purr</button>
Angular Parse Patch doesn't use $http on the backend so registering interceptors won't work for Parse calls. To get around this Angular Parse Patch will rootScope broadcast 'parse:transfer:start' and 'parse:transfer:end'.