Skip to content

[Refactor] abstract function that inject promises #18

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 2 commits 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store/
node_modules/
node_modules/
.vscode
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.

108 changes: 39 additions & 69 deletions src/parse-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,68 +51,40 @@
};

//// Let's loop over Parse objects
for (var k in methodsToUpdate) {

var currentClass = k;
var currentObject = methodsToUpdate[k];

var currentProtoMethods = currentObject.prototype;
var currentStaticMethods = currentObject.static;


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

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

// Overwrite original function by wrapping it with $q
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;
});


};

});


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

var origMethod = Parse[currentClass][method];

// Overwrite original function by wrapping it with $q
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;
});

};

});


for (var currentClass in methodsToUpdate) {
var prototypes = methodsToUpdate[currentClass].prototype,
statics = methodsToUpdate[currentClass].static,
current = Parse[currentClass];

if ( !angular.isUndefined(current) ) {
if ( !angular.isUndefined(current.prototype) ) {
promisfyQ(current.prototype, prototypes);
}
promisfyQ(current, statics);
}
}

}

function promisfyQ(object, methods) {

methods.forEach(function(method){
var origMethod = object[method];

// Overwrite original function by wrapping it with $q
object[method] = function() {
var defer = $q.defer();

origMethod.apply(this, arguments)
.then(defer.resolve, defer.reject);

return defer.promise;
};
});
}

}]);




Expand All @@ -131,12 +103,6 @@
return Parse.Object._classMap[className];
};

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


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

Expand All @@ -161,15 +127,14 @@
newClass.prototype['set' + field] = function(data) {
this.set(currentAttr, data);
return this;
}
};
}

});
}


return newClass;
}
};



Expand All @@ -194,7 +159,7 @@

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


/// Enhance Collection prototype
Expand All @@ -216,7 +181,7 @@

return this.query.find()
.then(function(newModels){
if (!opts || opts.add !== false) _this.add(newModels)
if (!opts || opts.add !== false) _this.add(newModels);
if (newModels.length < currentLimit) _this.hasMoreToLoad = false;
return newModels;
});
Expand All @@ -228,6 +193,11 @@
});

}

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

}]);

Expand Down