From 5aa1d80dae01258999c2e557468a38117387fa3f Mon Sep 17 00:00:00 2001 From: Jeronimo Vallelunga Date: Sat, 12 Dec 2015 11:47:47 -0300 Subject: [PATCH 1/2] [Refactor] abstract function that inject promises --- src/parse-angular.js | 74 ++++++++++++-------------------------------- 1 file changed, 20 insertions(+), 54 deletions(-) diff --git a/src/parse-angular.js b/src/parse-angular.js index 685b943..a27624d 100644 --- a/src/parse-angular.js +++ b/src/parse-angular.js @@ -51,65 +51,31 @@ }; //// 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){ + for (var currentClass in methodsToUpdate) { + var currentObject = methodsToUpdate[currentClass]; + + promisfyQ(Parse[currentClass].prototype, currentObject.prototype); + promisfyQ(Parse[currentClass], currentObject.static); + } + + function promisfyQ(object, methods) { + + methods.forEach(function(method){ - var origMethod = Parse[currentClass][method]; + var origMethod = object[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; - }); - + object[method] = function() { + var defer = $q.defer(); + + origMethod.apply(this, arguments) + .then(defer.resolve, defer.reject); + + return defer.promise; }; - }); - - - } + }); + } } }]); From 409d7af86e7ec9575ff1415b7d85ca22d21a26a3 Mon Sep 17 00:00:00 2001 From: Jeronimo Vallelunga Date: Sat, 12 Dec 2015 13:04:17 -0300 Subject: [PATCH 2/2] Identations fixed --- .gitignore | 3 +- dist/parse-angular.js | 2 +- src/parse-angular.js | 70 +++++++++++++++++++++++-------------------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index dd7e35f..12acf15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store/ -node_modules/ \ No newline at end of file +node_modules/ +.vscode \ No newline at end of file diff --git a/dist/parse-angular.js b/dist/parse-angular.js index fa014bc..7ba0570 100644 --- a/dist/parse-angular.js +++ b/dist/parse-angular.js @@ -1 +1 @@ -!function(t,e){var r=t.angular;if(r!==e){var n=r.module("parse-angular",[]);n.run(["$q","$window",function(t,e){if(!r.isUndefined(e.Parse)&&r.isObject(e.Parse)){var n=e.Parse,o={Object:{prototype:["save","fetch","destroy"],"static":["saveAll","destroyAll"]},Collection:{prototype:["fetch"],"static":[]},Query:{prototype:["find","first","count","get"],"static":[]},Cloud:{prototype:[],"static":["run"]},User:{prototype:["signUp"],"static":["requestPasswordReset","logIn"]},FacebookUtils:{prototype:[],"static":["logIn","link","unlink"]}};for(var i in o){var s=i,a=o[i],c=a.prototype,u=a.static;c.forEach(function(e){var r=n[s].prototype[e];n[s].prototype[e]=function(){return r.apply(this,arguments).then(function(e){var r=t.defer();return r.resolve(e),r.promise},function(e){var r=t.defer();return r.reject(e),r.promise})}}),u.forEach(function(e){var r=n[s][e];n[s][e]=function(){return r.apply(this,arguments).then(function(e){var r=t.defer();return r.resolve(e),r.promise},function(e){var r=t.defer();return r.reject(e),r.promise})}})}}}]),r.module("parse-angular.enhance",["parse-angular"]).run(["$q","$window",function(t,e){function n(t){return t.charAt(0).toUpperCase()+t.slice(1)}if(!r.isUndefined(e.Parse)&&r.isObject(e.Parse)){var o=e.Parse;o.Object.getClass=function(t){return o.Object._classMap[t]};var i=o.Object.extend;o.Object.extend=function(t){var e=i.apply(this,arguments);if(o._.isObject(t)&&o._.isArray(t.attrs)){var r=t.attrs;o._.each(r,function(t){var r=n(t);e.prototype["get"+r]||(e.prototype["get"+r]=function(){return this.get(t)}),e.prototype["set"+r]||(e.prototype["set"+r]=function(e){return this.set(t,e),this})})}return e},o.Collection._classMap={};var s=o.Collection.extend;o.Collection.extend=function(t){var e=s.apply(this,arguments);return t&&t.className&&(o.Collection._classMap[t.className]=e),e},o.Collection.getClass=function(t){return o.Collection._classMap[t]},o.Collection.prototype=r.extend(o.Collection.prototype,{loadMore:function(t){if(!r.isUndefined(this.query)){var e=-1==this.query._limit?100:this.query._limit,n=this.query._skip;n+=e,this.query.skip(n);var o=this;return this.query.find().then(function(r){return t&&t.add===!1||o.add(r),r.length