Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
refactor: move componentWrapper config methods to runtime service
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Apr 22, 2015
1 parent 38f0cd3 commit b49f2ca
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
35 changes: 21 additions & 14 deletions src/router-directive.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
angular.module('ngNewRouter', [])
.factory('$router', routerFactory)
.value('$routeParams', {})
.provider('$componentMapper', $componentMapperProvider)
.factory('$componentMapper', $componentMapperFactory)
.provider('$pipeline', pipelineProvider)
.factory('$$pipeline', privatePipelineFactory)
.factory('$setupRoutersStep', setupRoutersStepFactory)
Expand Down Expand Up @@ -485,7 +485,7 @@ function pipelineProvider() {


/**
* @name $componentMapperProvider
* @name $componentMapperFactory
* @description
*
* This lets you configure conventions for what controllers are named and where to load templates from.
Expand All @@ -500,7 +500,7 @@ function pipelineProvider() {
*
* This service makes it easy to group all of them into a single concept.
*/
function $componentMapperProvider() {
function $componentMapperFactory() {

var DEFAULT_SUFFIX = 'Controller';

Expand All @@ -522,17 +522,24 @@ function $componentMapperProvider() {
};

return {
$get: function () {
return {
controllerName: componentToCtrl,
controllerAs: componentToControllerAs,
template: componentToTemplate,
component: ctrlToComponent
};
controllerName: function (name) {
return componentToCtrl(name);
},

controllerAs: function (name) {
return componentToControllerAs(name);
},

template: function (name) {
return componentToTemplate(name);
},

component: function (name) {
return ctrlToComponent(name);
},

/**
* @name $componentMapperProvider#setCtrlNameMapping
* @name $componentMapper#setCtrlNameMapping
* @description takes a function for mapping component names to component controller names
*/
setCtrlNameMapping: function(newFn) {
Expand All @@ -541,7 +548,7 @@ function $componentMapperProvider() {
},

/**
* @name $componentMapperProvider#setCtrlAsMapping
* @name $componentMapper#setCtrlAsMapping
* @description takes a function for mapping component names to controllerAs name in the template
*/
setCtrlAsMapping: function(newFn) {
Expand All @@ -550,7 +557,7 @@ function $componentMapperProvider() {
},

/**
* @name $componentMapperProvider#setComponentFromCtrlMapping
* @name $componentMapper#setComponentFromCtrlMapping
* @description takes a function for mapping component controller names to component names
*/
setComponentFromCtrlMapping: function (newFn) {
Expand All @@ -559,7 +566,7 @@ function $componentMapperProvider() {
},

/**
* @name $componentMapperProvider#setTemplateMapping
* @name $componentMapper#setTemplateMapping
* @description takes a function for mapping component names to component template URLs
*/
setTemplateMapping: function(newFn) {
Expand Down
22 changes: 10 additions & 12 deletions test/component-mapper.es5.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@ describe('$componentMapper', function () {
expect($componentMapper.template('foo')).toBe('./components/foo/foo.html');
}));

it('should work with a controller constructor fn and a template url', function () {
it('should work with a controller constructor fn and a template url', inject(function ($componentMapper) {
var routes = {};
module(function($componentMapperProvider) {
$componentMapperProvider.setCtrlNameMapping(function (name) {
return routes[name].controller;
});
$componentMapperProvider.setTemplateMapping(function (name) {
return routes[name].templateUrl;
});
$componentMapperProvider.setCtrlAsMapping(function (name) {
return 'ctrl';
});
$componentMapper.setCtrlNameMapping(function (name) {
return routes[name].controller;
});
$componentMapper.setTemplateMapping(function (name) {
return routes[name].templateUrl;
});
$componentMapper.setCtrlAsMapping(function (name) {
return 'ctrl';
});

routes.myComponent = {
Expand Down Expand Up @@ -65,7 +63,7 @@ describe('$componentMapper', function () {
$rootScope.$digest();

expect(elt.text()).toBe('howdy');
});
}));

function compile(template) {
elt = $compile('<div>' + template + '</div>')($rootScope);
Expand Down

0 comments on commit b49f2ca

Please sign in to comment.