Skip to content
This repository has been archived by the owner on Jun 19, 2018. It is now read-only.

Commit

Permalink
feat(customTemplates): show a warning when a template doesnt exist in…
Browse files Browse the repository at this point in the history
… the template cache

Closes #422
  • Loading branch information
Matt Lewis committed Aug 10, 2016
1 parent c84e9ab commit a7eee2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/directives/mwlDynamicDirectiveTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ var angular = require('angular');

angular
.module('mwl.calendar')
.controller('MwlDynamicDirectiveTemplateCtrl', function($compile, $scope, $attrs, $element, $templateCache, calendarConfig) {
.controller('MwlDynamicDirectiveTemplateCtrl', function($compile, $scope, $attrs, $element, $templateCache, $log, calendarConfig) {

$scope.$watch($attrs.overrides, function(overrides) {

var templateName = calendarConfig.templates[$attrs.name];
if (
overrides &&
angular.isObject(overrides) &&
overrides[$attrs.name] &&
$templateCache.get(overrides[$attrs.name])
overrides[$attrs.name]
) {
templateName = overrides[$attrs.name];
if ($templateCache.get(overrides[$attrs.name])) {
templateName = overrides[$attrs.name];
} else {
$log.warn('Bootstrap Calendar', 'The custom template for ' + overrides[$attrs.name] +
' was not found in the template cache. Please ensure it is pre-loaded via a script tag ' +
'<script type="text/ng-template" id="' + overrides[$attrs.name] + '">Custom template content</script> or ' +
'via a tool such as https://www.npmjs.com/package/gulp-angular-templatecache');
}
}
var template = $templateCache.get(templateName);
$element.html(template);
Expand Down
14 changes: 12 additions & 2 deletions test/unit/directives/mwlDynamicDirectiveTemplate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ describe('dynamicDirectiveTemplate directive', function() {

beforeEach(angular.mock.module('mwl.calendar'));

var scope, elm, calendarConfig, $templateCache;
beforeEach(inject(function($rootScope, $compile, _$templateCache_, _calendarConfig_) {
var scope, elm, calendarConfig, $templateCache, $log;
beforeEach(inject(function($rootScope, $compile, _$templateCache_, _calendarConfig_, _$log_) {
$templateCache = _$templateCache_;
calendarConfig = _calendarConfig_;
$log = _$log_;
scope = $rootScope.$new();
calendarConfig.templates = {
foo: 'foo.html'
Expand Down Expand Up @@ -46,4 +47,13 @@ describe('dynamicDirectiveTemplate directive', function() {
expect(elm.text()).to.equal('bar baz');
});

it('should log a warning when the template is not in the cache', function() {
$log.warn = sinon.spy();
scope.overrides = {
foo: 'bam.html'
};
scope.$apply();
expect($log.warn).to.have.been.calledOnce;
});

});

0 comments on commit a7eee2a

Please sign in to comment.