Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 16363d8

Browse files
vojtajinaIgorMinar
authored andcommitted
refactor(ng:view, ng:include): pass cache instance into $http
Instead of doing all the stuff in these widgets (checking cache, etc..) we can rely on $http now...
1 parent 92995bb commit 16363d8

File tree

2 files changed

+63
-62
lines changed

2 files changed

+63
-62
lines changed

src/widgets.js

+19-40
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,6 @@ angularWidget('ng:include', function(element){
112112
useScope = scope.$eval(scopeExp),
113113
fromCache;
114114

115-
function updateContent(content) {
116-
element.html(content);
117-
if (useScope) {
118-
childScope = useScope;
119-
} else {
120-
releaseScopes.push(childScope = scope.$new());
121-
}
122-
compiler.compile(element)(childScope);
123-
$autoScroll();
124-
scope.$eval(onloadExp);
125-
}
126-
127115
function clearContent() {
128116
childScope = null;
129117
element.html('');
@@ -133,16 +121,17 @@ angularWidget('ng:include', function(element){
133121
releaseScopes.pop().$destroy();
134122
}
135123
if (src) {
136-
if ((fromCache = $templateCache.get(src))) {
137-
scope.$evalAsync(function() {
138-
updateContent(fromCache);
139-
});
140-
} else {
141-
$http.get(src).on('success', function(response) {
142-
updateContent(response);
143-
$templateCache.put(src, response);
144-
}).on('error', clearContent);
145-
}
124+
$http.get(src, {cache: $templateCache}).on('success', function(response) {
125+
element.html(response);
126+
if (useScope) {
127+
childScope = useScope;
128+
} else {
129+
releaseScopes.push(childScope = scope.$new());
130+
}
131+
compiler.compile(element)(childScope);
132+
$autoScroll();
133+
scope.$eval(onloadExp);
134+
}).on('error', clearContent);
146135
} else {
147136
clearContent();
148137
}
@@ -586,30 +575,20 @@ angularWidget('ng:view', function(element) {
586575
var template = $route.current && $route.current.template,
587576
fromCache;
588577

589-
function updateContent(content) {
590-
element.html(content);
591-
compiler.compile(element)($route.current.scope);
592-
}
593-
594578
function clearContent() {
595579
element.html('');
596580
}
597581

598582
if (template) {
599-
if ((fromCache = $templateCache.get(template))) {
600-
scope.$evalAsync(function() {
601-
updateContent(fromCache);
602-
});
603-
} else {
604-
// xhr's callback must be async, see commit history for more info
605-
$http.get(template).on('success', function(response) {
606-
// ignore callback if another route change occured since
607-
if (newChangeCounter == changeCounter)
608-
updateContent(response);
609-
$templateCache.put(template, response);
583+
// xhr's callback must be async, see commit history for more info
584+
$http.get(template, {cache: $templateCache}).on('success', function(response) {
585+
// ignore callback if another route change occured since
586+
if (newChangeCounter == changeCounter) {
587+
element.html(response);
588+
compiler.compile(element)($route.current.scope);
610589
$autoScroll();
611-
}).on('error', clearContent);
612-
}
590+
}
591+
}).on('error', clearContent);
613592
} else {
614593
clearContent();
615594
}

test/widgetsSpec.js

+44-22
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,37 @@ describe("widget", function() {
5656

5757

5858
describe('ng:include', inject(function($rootScope, $compile) {
59-
it('should include on external file', inject(function($rootScope, $compile, $cacheFactory) {
59+
60+
function putIntoCache(url, content) {
61+
return function($templateCache) {
62+
$templateCache.put(url, [200, content, {}]);
63+
};
64+
}
65+
66+
67+
it('should include on external file', inject(putIntoCache('myUrl', '{{name}}'),
68+
function($rootScope, $compile, $browser) {
6069
var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>');
6170
var element = $compile(element)($rootScope);
6271
$rootScope.childScope = $rootScope.$new();
6372
$rootScope.childScope.name = 'misko';
6473
$rootScope.url = 'myUrl';
65-
$cacheFactory.get('templates').put('myUrl', '{{name}}');
6674
$rootScope.$digest();
75+
$browser.defer.flush();
6776
expect(element.text()).toEqual('misko');
6877
}));
6978

70-
71-
it('should remove previously included text if a falsy value is bound to src',
72-
inject(function($rootScope, $compile, $cacheFactory) {
79+
80+
it('should remove previously included text if a falsy value is bound to src', inject(
81+
putIntoCache('myUrl', '{{name}}'),
82+
function($rootScope, $compile, $browser) {
7383
var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>');
7484
var element = $compile(element)($rootScope);
7585
$rootScope.childScope = $rootScope.$new();
7686
$rootScope.childScope.name = 'igor';
7787
$rootScope.url = 'myUrl';
78-
$cacheFactory.get('templates').put('myUrl', '{{name}}');
7988
$rootScope.$digest();
89+
$browser.defer.flush();
8090

8191
expect(element.text()).toEqual('igor');
8292

@@ -86,13 +96,15 @@ describe("widget", function() {
8696
expect(element.text()).toEqual('');
8797
}));
8898

89-
90-
it('should allow this for scope', inject(function($rootScope, $compile, $cacheFactory) {
99+
100+
it('should allow this for scope', inject(putIntoCache('myUrl', '{{"abc"}}'),
101+
function($rootScope, $compile, $browser) {
91102
var element = jqLite('<ng:include src="url" scope="this"></ng:include>');
92103
var element = $compile(element)($rootScope);
93104
$rootScope.url = 'myUrl';
94-
$cacheFactory.get('templates').put('myUrl', '{{"abc"}}');
95105
$rootScope.$digest();
106+
$browser.defer.flush();
107+
96108
// TODO(misko): because we are using scope==this, the eval gets registered
97109
// during the flush phase and hence does not get called.
98110
// I don't think passing 'this' makes sense. Does having scope on ng:include makes sense?
@@ -102,39 +114,43 @@ describe("widget", function() {
102114
expect(element.text()).toEqual('abc');
103115
}));
104116

105-
106-
it('should evaluate onload expression when a partial is loaded',
107-
inject(function($rootScope, $compile, $cacheFactory) {
117+
118+
it('should evaluate onload expression when a partial is loaded', inject(
119+
putIntoCache('myUrl', 'my partial'),
120+
function($rootScope, $compile, $browser) {
108121
var element = jqLite('<ng:include src="url" onload="loaded = true"></ng:include>');
109122
var element = $compile(element)($rootScope);
110123

111124
expect($rootScope.loaded).not.toBeDefined();
112125

113126
$rootScope.url = 'myUrl';
114-
$cacheFactory.get('templates').put('myUrl', 'my partial');
115127
$rootScope.$digest();
128+
$browser.defer.flush();
129+
116130
expect(element.text()).toEqual('my partial');
117131
expect($rootScope.loaded).toBe(true);
118132
}));
119133

120-
121-
it('should destroy old scope', inject(function($rootScope, $compile, $cacheFactory) {
134+
135+
it('should destroy old scope', inject(putIntoCache('myUrl', 'my partial'),
136+
function($rootScope, $compile, $browser) {
122137
var element = jqLite('<ng:include src="url"></ng:include>');
123138
var element = $compile(element)($rootScope);
124139

125140
expect($rootScope.$$childHead).toBeFalsy();
126141

127142
$rootScope.url = 'myUrl';
128-
$cacheFactory.get('templates').put('myUrl', 'my partial');
129143
$rootScope.$digest();
144+
$browser.defer.flush();
130145
expect($rootScope.$$childHead).toBeTruthy();
131146

132147
$rootScope.url = null;
133148
$rootScope.$digest();
134149
expect($rootScope.$$childHead).toBeFalsy();
135150
}));
136151

137-
it('should do xhr request and cache it', inject(function($rootScope, $httpBackend, $compile) {
152+
it('should do xhr request and cache it',
153+
inject(function($rootScope, $httpBackend, $compile, $browser) {
138154
var element = $compile('<ng:include src="url"></ng:include>')($rootScope);
139155
$httpBackend.expect('GET', 'myUrl').respond('my partial');
140156

@@ -149,6 +165,7 @@ describe("widget", function() {
149165

150166
$rootScope.url = 'myUrl';
151167
$rootScope.$digest();
168+
$browser.defer.flush();
152169
expect(element.text()).toEqual('my partial');
153170
dealoc($rootScope);
154171
}));
@@ -165,11 +182,12 @@ describe("widget", function() {
165182
expect(element.text()).toBe('');
166183
}));
167184

168-
it('should be async even if served from cache', inject(function($rootScope, $compile, $cacheFactory) {
185+
it('should be async even if served from cache', inject(
186+
putIntoCache('myUrl', 'my partial'),
187+
function($rootScope, $compile, $browser) {
169188
var element = $compile('<ng:include src="url"></ng:include>')($rootScope);
170189

171190
$rootScope.url = 'myUrl';
172-
$cacheFactory.get('templates').put('myUrl', 'my partial');
173191

174192
var called = 0;
175193
// we want to assert only during first watch
@@ -178,6 +196,7 @@ describe("widget", function() {
178196
});
179197

180198
$rootScope.$digest();
199+
$browser.defer.flush();
181200
expect(element.text()).toBe('my partial');
182201
}));
183202
}));
@@ -574,7 +593,8 @@ describe("widget", function() {
574593
}));
575594

576595
it('should initialize view template after the view controller was initialized even when ' +
577-
'templates were cached', inject(function($rootScope, $compile, $location, $httpBackend, $route) {
596+
'templates were cached',
597+
inject(function($rootScope, $compile, $location, $httpBackend, $route, $browser) {
578598
//this is a test for a regression that was introduced by making the ng:view cache sync
579599

580600
$route.when('/foo', {controller: ParentCtrl, template: 'viewPartial.html'});
@@ -605,6 +625,7 @@ describe("widget", function() {
605625
$rootScope.log = [];
606626
$location.path('/foo');
607627
$rootScope.$apply();
628+
$browser.defer.flush();
608629

609630
expect($rootScope.log).toEqual(['parent', 'init', 'child']);
610631
}));
@@ -644,9 +665,9 @@ describe("widget", function() {
644665
}));
645666

646667
it('should be async even if served from cache',
647-
inject(function($route, $rootScope, $location, $cacheFactory) {
668+
inject(function($route, $rootScope, $location, $templateCache, $browser) {
669+
$templateCache.put('myUrl1', [200, 'my partial', {}]);
648670
$route.when('/foo', {controller: noop, template: 'myUrl1'});
649-
$cacheFactory.get('templates').put('myUrl1', 'my partial');
650671
$location.path('/foo');
651672

652673
var called = 0;
@@ -656,6 +677,7 @@ describe("widget", function() {
656677
});
657678

658679
$rootScope.$digest();
680+
$browser.defer.flush();
659681
expect(element.text()).toBe('my partial');
660682
}));
661683
});

0 commit comments

Comments
 (0)