function($routeProvider) {
$routeProvider.
when('/:module/:view', {
templateUrl: function(params) {
var core = angular.element(document).injector().get('core');
core.openView(params.module, params.view);
return 'app/views/{0}/{1}.jsp'.format(params.module, params.view);
}
}).
otherwise({
redirectTo: '/home/landing'
});
});
'use strict';
// declare an application core module
var coreModule = angular.module('core', ['network']);
coreModule.factory('core',
function (http, $location) {
return {
openView: function (module, view) {
if (!module) {
throw new Error('Please provide module.');
}
if (!view) {
throw new Error('Please provide view.');
}
http.getController(module, view);
if($location.path() !== '/{0}/{1}'.format(module, view)) {
$location.path('/{0}/{1}'.format(module, view)).replace();
}
}
};
}
);
'use strict';
// declare a network service module
var httpModule = angular.module('network', []);
httpModule.factory('http',
function ($http, $q) {
return {
get: function (url) {
if (!url) {
throw new Error('Please provide request url.');
}
var defer = $q.defer();
$http.get(url).then(
function(response) {
defer.resolve(response.data);
},
function(error) {
defer.reject(error);
});
return defer.promise;
},
post: function (url, paramObj) {
if (!url) {
throw new Error('Please provide request url.');
}
var defer = $q.defer();
paramObj = paramObj || {};
$http.post(url, paramObj).then(
function(response) {
defer.resolve(response.data);
},
function(error) {
defer.reject(error);
});
return defer.promise;
},
getController: function (module, controller) {
if (!module) {
throw new Error('Please provide module.');
}
if (!controller) {
throw new Error('Please provide controller.');
}
var webRoot = window.location.href.substr(0, window.location.href.lastIndexOf('/home.action'));
var url = '{0}/{1}/{2}/{3}.js'.format(webRoot, 'app/controllers', module, controller);
var existed = false;
$.each($('head').find('script'), function(index, value) {
if(value.src === url) {
existed = true;
}
});
if(!existed) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.charset = 'utf-8';
document.head.appendChild(script);
}
}
};
}
);