Skip to content

Commit 3c5c348

Browse files
author
Dean Sofer
committed
Merge branch 'ui-template-fixes' of git://github.com/dandoyon/angular-ui
2 parents be513c7 + 42e6e51 commit 3c5c348

File tree

6 files changed

+140
-129
lines changed

6 files changed

+140
-129
lines changed

templates/README.md

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,34 @@
11
# ui-template directives
22

3-
These directives are boilerplates for creating your own directives.
3+
These are boilerplates for creating filters and directives. They use uiConfig so that you can globally assign config options (see uiConfig doc).
4+
Currently there are only two templates, one for filter and one for directive along with simple a implementation of each.
45

56
## Usage
67

7-
Load the script file: template.js in your application:
8-
9-
<script type="text/javascript" src="modules/directives/template/src/template.js"></script>
10-
11-
Add the template module as a dependency to your application module:
12-
13-
var myAppModule = angular.module('MyApp', ['ui.directives.template'])
14-
15-
Apply the directive to your html elements:
16-
17-
<span ui-template num="SomeNumber"></span>
18-
19-
Default styles are in angular-ui.css and are pretty boring, you could just override these in your
20-
stylesheet and make things more interesting
21-
22-
### Options
23-
24-
All the options can be passed through the directive or set on the html element.
25-
NOTE: attributes override controller options
26-
27-
myAppModule.controller('MyController', function($scope) {
28-
$scope.SomeNumber = 123;
29-
$scope.uiTemplateOptions = {
30-
31-
};
32-
});
33-
34-
// two-way binding with default for scoped.options uiTemplateOptions
35-
<span ui-template ng-model="SomeNumber"></span>
36-
37-
// one way binding with your own name for scoped options
38-
<span ui-template options="myOptions" num="SomeNumber"></span>
39-
40-
41-
### Notes
42-
43-
ui-template
44-
- one-way binding unless you have in an ng-repeat
45-
- does not currently work with ng-model.
46-
- is supported only for attribute style elements
47-
48-
### Todo
49-
- support ng-model
50-
8+
Here's how the two template impls could be used
9+
<html>
10+
<head>
11+
<!-- your angular and other library imports here -->
12+
<script>
13+
// create global prefix/suffix for elements using wrap filter
14+
var myApp = angular.module('myApp',['ui'])
15+
.value('ui.config', { 'directiveTmpl': { 'somedirectiveopt' : 'myval' } });
16+
17+
function MyCtrl($scope) {
18+
$scope.mymodel = "a value";
19+
}
20+
</script>
21+
22+
</head>
23+
24+
<body ng-controller="MyCtrl">
25+
<div>
26+
<span>{{mymodel | filterTmpl}}</span>
27+
28+
<span ui-directive-template ng-model="mymodel"></span>
29+
<ui-directive-template ng-model="mymodel"></ui-directive-template>
30+
31+
</div>
32+
</body>
33+
34+
</html>

templates/src/template.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

templates/stylesheets/template.less

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/* ui-template */
1+
/* default styles (if any) for your directive implementations */
2+

templates/template.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Filter/directive templates
3+
*/
4+
angular.module('ui.filters')
5+
.filter('filterTmpl', ['ui.config', function(uiConfig) {
6+
var config = { 'somefilteropt' : 'foo'};
7+
if (uiConfig.filterTmpl) {
8+
angular.extend(config, uiConfig.filterTmpl);
9+
}
10+
return function (value, kind) {
11+
var opts = {}, inputs = {}, out;
12+
// if an input is given then override
13+
if(kind) {
14+
inputs = {'kind' : kind };
15+
}
16+
angular.extend(opts, config, inputs);
17+
//
18+
// do something more interesting to input value here
19+
//
20+
out = value;
21+
return out;
22+
};
23+
}]);
24+
25+
angular.module('ui.directives')
26+
.directive('uiDirectiveTmpl', ['ui.config', function(uiConfig) {
27+
var config = { 'somedirectiveopt' : 'bar' };
28+
if (uiConfig.directiveTmpl) {
29+
angular.extend(config, uiConfig.directiveTmpl);
30+
}
31+
return {
32+
restrict: 'EAC', // supports using directive as element, attribute and class
33+
require: 'ngModel', // requires ng-model controller so we get the two-way binding
34+
link: function(scope, element, attrs, ngModel) {
35+
var opts = {}; // instance-specific options
36+
37+
// build up our options
38+
angular.extend(opts, config, scope.$eval(attrs.uiTmpl)); // element attributes that override default options
39+
40+
// override the controllers render function and do something interesting in here
41+
ngModel.$render = function() {
42+
element.text(ngModel.$viewValue); // boring
43+
};
44+
}
45+
};
46+
}]);

templates/test/templateSpec.js

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,70 @@
1-
describe('uiTemplate', function() {
2-
var scope;
3-
scope = null;
1+
/*
2+
* sample unit testing for sample templates and implementations
3+
*/
4+
describe('uiTemplates', function() {
5+
6+
// declare these up here to be global to all tests
7+
var $rootScope, $compile;
8+
9+
// override uiConfig for testing purposes, this definition will clobber anything already predefined ... so be careful
10+
angular.module('ui.config', [])
11+
.value('ui.config', {
12+
'filterTmpl' : { 'somefilteropt' : 'foo' },
13+
'directiveTmpl' : { 'somedirectiveopt' : 'bar' }
14+
});
15+
416
beforeEach(module('ui.directives'));
5-
beforeEach(inject(function($rootScope) {
6-
scope = $rootScope.$new();
17+
beforeEach(module('ui.filters'));
18+
19+
// inject in angular constructs. Injector knows about leading/trailing underscores and does the right thing
20+
// otherwise, you would need to inject these into each test
21+
beforeEach(inject(function(_$rootScope_, _$compile_, _$filter_) {
22+
$rootScope = _$rootScope_;
23+
$compile = _$compile_;
724
}));
8-
describe('use on a div element with two-way binding', function() {
9-
it('(two-way) should render MODELVAL in template ', function() {
10-
inject(function($compile) {
11-
var element;
12-
element = $compile("<div ui-template ng-model='mymodel'></div>")(scope);
13-
scope.$apply(function() {
14-
scope.mymodel = 'MODELVAL';
15-
});
16-
expect(element.text()).toEqual('MODELVAL');
25+
26+
describe('filter tests', function() {
27+
28+
// we're doing filter tests so globally define here for this subset of tests
29+
var $filter;
30+
beforeEach(inject(function(_$filter_) {
31+
$filter = _$filter_;
32+
}));
33+
34+
// very simple boilerplate setup of test for a custom filter
35+
describe('filterTmpl should', function() {
36+
var tmpl;
37+
beforeEach(function() {
38+
tmpl = $filter('filterTmpl');
1739
});
18-
});
19-
// TODO: Need test for checking default attribute values
20-
});
21-
describe('use on a div element with one-way binding', function() {
22-
it('(one-way) should render someattr value in template', function() {
23-
inject(function($compile) {
24-
var element;
25-
scope.mymodel = 'ATTRVAL';
26-
element = $compile("<div ui-template somemodel='mymodel'></div>")(scope);
27-
expect(element.text()).toEqual('ATTRVAL');
40+
41+
it('exist when provided', function() {
42+
expect(tmpl).toBeDefined();
2843
});
44+
45+
it('return exactly what interesting thing the filter is doing to input', function() {
46+
expect(tmpl('text')).toEqual('text');
47+
});
48+
2949
});
3050
});
31-
describe('use as tag with two-way binding', function() {
32-
it('(two-way) should render MODELVAL in template ', function() {
33-
inject(function($compile) {
34-
var element;
35-
element = $compile("<ui-template ng-model='mymodel'></ui-template>")(scope);
36-
scope.$apply(function() {
37-
scope.mymodel = 'MODELVAL';
38-
});
39-
expect(element.text()).toEqual('MODELVAL');
51+
52+
describe('directive tests', function() {
53+
var element;
54+
describe('uiDirectiveTmpl should', function() {
55+
it('create an element if using element-style', function() {
56+
var element = $compile('<ui-directive-tmpl ng-model="a"></ui-directive-tmpl>')($rootScope);
57+
expect(element).toBeDefined();
58+
});
59+
it('render the models value in element', function() {
60+
var element = $compile('<div ui-directive-tmpl ng-model="a"></div>')($rootScope);
61+
expect(element.text()).toEqual('');
62+
$rootScope.a = 'foo';
63+
$rootScope.$digest();
64+
expect(element.text()).toEqual('foo');
4065
});
4166
});
67+
4268
});
69+
4370
});

test/test-config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ files = [
1818
'test/lib/codemirror/codemirror.js',
1919
'test/lib/googlemaps/googlemaps.js',
2020
'build/angular-ui.js',
21-
'modules/*/*/test/*.js'
21+
'modules/*/*/test/*.js',
22+
'templates/*.js',
23+
'templates/*/test/*.js'
2224
];
2325

2426
// list of files to exclude

0 commit comments

Comments
 (0)