Skip to content

Added support for properties #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/parse-angular.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

209 changes: 104 additions & 105 deletions src/parse-angular.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
(function(window, undef){
(function (window, undef) {

var angular = window.angular;

if (angular !== undef) {

var module = angular.module('parse-angular', []);

module.run(['$q', '$window', function($q, $window){
module.run(['$q', '$window', function ($q, $window) {


// Process only if Parse exist on the global window, do nothing otherwise
Expand Down Expand Up @@ -61,176 +61,175 @@


/// Patching prototypes
currentProtoMethods.forEach(function(method){
currentProtoMethods.forEach(function (method) {

var origMethod = Parse[currentClass].prototype[method];

// Overwrite original function by wrapping it with $q
Parse[currentClass].prototype[method] = function() {
Parse[currentClass].prototype[method] = function () {

return origMethod.apply(this, arguments)
.then(function(data){
var defer = $q.defer();
defer.resolve(data);
return defer.promise;
}, function(err){
var defer = $q.defer();
defer.reject(err);
return defer.promise;
});


.then(function (data) {
var defer = $q.defer();
defer.resolve(data);
return defer.promise;
}, function (err) {
var defer = $q.defer();
defer.reject(err);
return defer.promise;
});
};

});


///Patching static methods too
currentStaticMethods.forEach(function(method){
currentStaticMethods.forEach(function (method) {

var origMethod = Parse[currentClass][method];

// Overwrite original function by wrapping it with $q
Parse[currentClass][method] = function() {
Parse[currentClass][method] = function () {

return origMethod.apply(this, arguments)
.then(function(data){
var defer = $q.defer();
defer.resolve(data);
return defer.promise;
}, function(err){
var defer = $q.defer();
defer.reject(err);
return defer.promise;
});

.then(function (data) {
var defer = $q.defer();
defer.resolve(data);
return defer.promise;
}, function (err) {
var defer = $q.defer();
defer.reject(err);
return defer.promise;
});
};

});


}
}

}]);



angular.module('parse-angular.enhance', ['parse-angular'])
.run(['$q', '$window', function($q, $window){
.run(['$q', '$window', function ($q, $window) {


if (!angular.isUndefined($window.Parse) && angular.isObject($window.Parse)) {
if (!angular.isUndefined($window.Parse) && angular.isObject($window.Parse)) {

var Parse = $window.Parse;
var Parse = $window.Parse;

/// Create a method to easily access our object
/// Because Parse.Object("xxxx") is actually creating an object and we can't access static methods
/// Create a method to easily access our object
/// Because Parse.Object("xxxx") is actually creating an object and we can't access static methods

Parse.Object.getClass = function(className) {
return Parse.Object._classMap[className];
};
Parse.Object.getClass = function (className) {
return Parse.Object._classMap[className];
};

///// CamelCaseIsh Helper
function capitaliseFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
///// CamelCaseIsh Helper
var capitaliseFirstLetter = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};


///// Override orig extend
var origObjectExtend = Parse.Object.extend;
///// Override orig extend
var origObjectExtend = Parse.Object.extend;

Parse.Object.extend = function(protoProps) {
Parse.Object.extend = function (protoProps) {

var newClass = origObjectExtend.apply(this, arguments);
var newClass = origObjectExtend.apply(this, arguments);

if (Parse._.isObject(protoProps) && Parse._.isArray(protoProps.attrs)) {
var attrs = protoProps.attrs;
/// Generate setters & getters
Parse._.each(attrs, function(currentAttr){
if (Parse._.isObject(protoProps) && Parse._.isArray(protoProps.attrs)) {
var attrs = protoProps.attrs;
/// Generate setters & getters
Parse._.each(attrs, function (currentAttr) {

var field = capitaliseFirstLetter(currentAttr);
var field = capitaliseFirstLetter(currentAttr);

// Don't override if we set a custom setters or getters
if(!newClass.prototype['get' + field]) {
newClass.prototype['get' + field] = function() {
return this.get(currentAttr);
};
}
if(!newClass.prototype['set' + field]) {
newClass.prototype['set' + field] = function(data) {
this.set(currentAttr, data);
return this;
// Don't override if we set a custom setters or getters
if (!newClass.prototype['get' + field]) {
newClass.prototype['get' + field] = function () {
return this.get(currentAttr);
};
}
if (!newClass.prototype['set' + field]) {
newClass.prototype['set' + field] = function (data) {
this.set(currentAttr, data);
return this;
};
}
}

});
}
var prop = Object.getOwnPropertyDescriptor(newClass.prototype, currentAttr);
if (!prop) {
newClass.prototype.__defineGetter__(currentAttr, function () {
return this.get(currentAttr);
});
newClass.prototype.__defineSetter__(currentAttr, function (value) {
return this.set(currentAttr, value);
});
}
});
}


return newClass;
}
return newClass;
};


/// Keep references & init collection class map
Parse.Collection._classMap = {};

/// Keep references & init collection class map
Parse.Collection._classMap = {};
var origExtend = Parse.Collection.extend;

var origExtend = Parse.Collection.extend;
/// Enhance Collection 'extend' to store their subclass in a map
Parse.Collection.extend = function (opts) {

/// Enhance Collection 'extend' to store their subclass in a map
Parse.Collection.extend = function(opts) {
var extended = origExtend.apply(this, arguments);

var extended = origExtend.apply(this, arguments);
if (opts && opts.className) {
Parse.Collection._classMap[opts.className] = extended;
}

if (opts && opts.className) {
Parse.Collection._classMap[opts.className] = extended;
}
return extended;

return extended;
};

};

Parse.Collection.getClass = function (className) {
return Parse.Collection._classMap[className];
};

Parse.Collection.getClass = function(className) {
return Parse.Collection._classMap[className];
}

/// Enhance Collection prototype
Parse.Collection.prototype = angular.extend(Parse.Collection.prototype, {
// Simple paginator
loadMore: function (opts) {

/// Enhance Collection prototype
Parse.Collection.prototype = angular.extend(Parse.Collection.prototype, {
// Simple paginator
loadMore: function(opts) {
if (!angular.isUndefined(this.query)) {

if (!angular.isUndefined(this.query)) {
// Default Parse limit is 100
var currentLimit = this.query._limit == -1 ? 100 : this.query._limit;
var currentSkip = this.query._skip;

// Default Parse limit is 100
var currentLimit = this.query._limit == -1 ? 100 : this.query._limit;
var currentSkip = this.query._skip;
currentSkip += currentLimit;

currentSkip += currentLimit;
this.query.skip(currentSkip);

this.query.skip(currentSkip);
var _this = this;

var _this = this;
return this.query.find()
.then(function (newModels) {
if (!opts || opts.add !== false) _this.add(newModels);
if (newModels.length < currentLimit) _this.hasMoreToLoad = false;
return newModels;
});

return this.query.find()
.then(function(newModels){
if (!opts || opts.add !== false) _this.add(newModels)
if (newModels.length < currentLimit) _this.hasMoreToLoad = false;
return newModels;
});
}

}

}

});

}
});

}]);
}

}]);


}
Expand Down