Created
February 9, 2015 21:41
-
-
Save zontafil/4949b489b0868f60f17f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Sjuga = Sjuga || {}; | |
| Sjuga.site.factory('sitePage', ['$resource', 'siteGlobal', '$q', | |
| function($resource, siteGlobal, $q) { | |
| // $resource wrapper that elaborates data after querying server | |
| return { | |
| query: query, | |
| get: get | |
| } | |
| function query(filter,cb_success,cb_fail){ return _callResource('query',filter,cb_success,cb_fail);} | |
| function get(filter,cb_success,cb_fail){ return _callResource('get',filter,cb_success,cb_fail);} | |
| function _callResource(method,filter,cb_success,cb_fail){ | |
| var deferred = $q.defer(); | |
| //keep trace if data has been processed yet | |
| var processed = false; | |
| var res = $resource(siteGlobal.api + 'page/:idPage/:what/:id/', { | |
| what: '@what', | |
| id: '@id', | |
| idPage: '@idPage' | |
| }) | |
| [method](filter,function(data){ | |
| //success callback | |
| if (!processed) data = processData(data); | |
| if (typeof(cb_success)=='function') cb_success(data); | |
| },cb_fail); | |
| res.$promise | |
| .then( | |
| function(data){ | |
| //success promise callback | |
| if (!processed) data = processData(data); | |
| deferred.resolve(data); | |
| }, | |
| function(err){deferred.reject(err);} | |
| ); | |
| function processData(data){ | |
| //elaborate data | |
| processed = true; | |
| //break lines | |
| if (siteGlobal.filters.lineBreak){ | |
| if (data.length) data.forEach(function(item){ | |
| if (!!item.message) item.message = item.message.replace(/\n+/g,"<br>"); | |
| }) | |
| if (!!data.description) data.description = data.description.replace(/\n+/g,"<br>") | |
| if (!!data.about) data.about = data.about.replace(/\n+/g,"<br>") | |
| if (!!data.mission) data.mission = data.mission.replace(/\n+/g,"<br>") | |
| if ((!!data.milestones) && (data.milestones.length)) data.milestones.forEach(function(item){ | |
| if (!!item.description) item.description = item.description.replace(/\n+/g,"<br>"); | |
| }) | |
| } | |
| //linkify strings | |
| if (siteGlobal.filters.linkify){ | |
| if (data.length) data.forEach(function(item){ | |
| if (!!item.message) item.message = linkify(item.message); | |
| }); | |
| if (!!data.description) data.description = linkify(data.description); | |
| if (!!data.about) data.about = linkify(data.about); | |
| if (!!data.mission) data.mission = linkify(data.mission); | |
| if ((!!data.milestones) && (data.milestones.length)) data.milestones.forEach(function(item){ | |
| if (!!item.description) item.description = linkify(item.description); | |
| }) | |
| } | |
| //inject data into the returned promise | |
| angular.extend(deferred.promise,data); | |
| return data; | |
| } | |
| //inject our wrapped promise into $resource return object | |
| res.$promise = deferred.promise; | |
| return res; | |
| } | |
| } | |
| ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment