feat($templateCache): add $templateCache - shared by ng:include, ng:view

This commit is contained in:
Vojta Jina 2011-10-18 16:35:32 -07:00 committed by Igor Minar
parent cd28a2e952
commit 4d2d70e7fb
3 changed files with 17 additions and 14 deletions

View file

@ -87,6 +87,7 @@ function ngModule($provide, $injector) {
$provide.service('$routeParams', $RouteParamsProvider);
$provide.service('$rootScope', $RootScopeProvider);
$provide.service('$sniffer', $SnifferProvider);
$provide.service('$templateCache', $TemplateCacheProvider);
$provide.service('$window', $WindowProvider);
}

View file

@ -150,3 +150,10 @@ function $CacheFactoryProvider() {
return cacheFactory;
};
}
function $TemplateCacheProvider() {
this.$get = ['$cacheFactory', function($cacheFactory) {
return $cacheFactory('templates');
}];
}

View file

@ -90,15 +90,13 @@ angularWidget('ng:include', function(element){
this.directives(true);
} else {
element[0]['ng:compiled'] = true;
return ['$http', '$cacheFactory', '$autoScroll', '$element',
function($http, $cacheFactory, $autoScroll, element) {
return ['$http', '$templateCache', '$autoScroll', '$element',
function($http, $templateCache, $autoScroll, element) {
var scope = this,
changeCounter = 0,
releaseScopes = [],
childScope,
oldScope,
// TODO(vojta): configure the cache / extract into $tplCache service ?
cache = $cacheFactory.get('templates') || $cacheFactory('templates');
oldScope;
function incrementChange() { changeCounter++;}
this.$watch(srcExp, incrementChange);
@ -135,14 +133,14 @@ angularWidget('ng:include', function(element){
releaseScopes.pop().$destroy();
}
if (src) {
if ((fromCache = cache.get(src))) {
if ((fromCache = $templateCache.get(src))) {
scope.$evalAsync(function() {
updateContent(fromCache);
});
} else {
$http.get(src).on('success', function(response) {
updateContent(response);
cache.put(src, response);
$templateCache.put(src, response);
}).on('error', clearContent);
}
} else {
@ -575,14 +573,11 @@ angularWidget('ng:view', function(element) {
if (!element[0]['ng:compiled']) {
element[0]['ng:compiled'] = true;
return ['$http', '$cacheFactory', '$route', '$autoScroll', '$element',
function($http, $cacheFactory, $route, $autoScroll, element) {
return ['$http', '$templateCache', '$route', '$autoScroll', '$element',
function($http, $templateCache, $route, $autoScroll, element) {
var template;
var changeCounter = 0;
// TODO(vojta): configure the cache / extract into $tplCache service ?
var cache = $cacheFactory.get('templates') || $cacheFactory('templates');
this.$on('$afterRouteChange', function() {
changeCounter++;
});
@ -601,7 +596,7 @@ angularWidget('ng:view', function(element) {
}
if (template) {
if ((fromCache = cache.get(template))) {
if ((fromCache = $templateCache.get(template))) {
scope.$evalAsync(function() {
updateContent(fromCache);
});
@ -611,7 +606,7 @@ angularWidget('ng:view', function(element) {
// ignore callback if another route change occured since
if (newChangeCounter == changeCounter)
updateContent(response);
cache.put(template, response);
$templateCache.put(template, response);
$autoScroll();
}).on('error', clearContent);
}