Skip to content

Instantly share code, notes, and snippets.

@jehugaleahsa
Created September 11, 2014 15:23
Show Gist options
  • Save jehugaleahsa/df9503b064ea1de702bb to your computer and use it in GitHub Desktop.
Save jehugaleahsa/df9503b064ea1de702bb to your computer and use it in GitHub Desktop.
Navigator Using Earl.js
application.factory('navigator', ['$http', '$window', 'baseUrl', function ($http, $window, baseUrl) {
function enrich(urlTemplate) {
if (!urlTemplate) {
return null;
}
var template = earl(urlTemplate);
var resource = {
urlTemplate: urlTemplate,
url: function (params) { return template.expand(params); }
};
return resource;
}
function enrichPage(urlTemplate) {
var enriched = enrich(urlTemplate);
if (enriched) {
enriched.redirect = function (params) {
$window.location.href = this.url(params || {});
}
}
return enriched;
}
function enrichApi(urlTemplate, actions) {
var enriched = enrich(urlTemplate);
if (enriched) {
actions = actions || {};
for (var action in actions) {
var method = actions[action];
enriched[action] = function (params) {
return $http({ method: method, url: enriched.url(params || {}) });
}
}
}
return enriched;
}
var navigator = { pages: {}, api: {} };
navigator.pages.childAccounts = enrichPage(baseUrl + 'childaccounts/{childId}');
navigator.api.lookups = enrichApi(baseUrl + 'api/lookups', { get: 'GET' });
navigator.api.searchAccounts = enrichApi(baseUrl + 'api/searchaccounts', { get: 'GET' });
navigator.api.childAccounts = enrichApi(baseUrl + 'api/childaccounts/{childId}', { get: 'GET' });
return navigator;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment