Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/service/xhr.cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@
* @param {boolean=} [verifyCache=false] If `true` then a result is immediately returned from cache
* (if present) while a request is sent to the server for a fresh response that will update the
* cached entry. The `callback` function will be called when the response is received.
* @param {boolean=} [sync=false] in case of cache hit execute `callback` synchronously.
*/
angularServiceInject('$xhr.cache', function($xhr, $defer, $log){
var inflight = {}, self = this;
function cache(method, url, post, callback, verifyCache){
function cache(method, url, post, callback, verifyCache, sync){
if (isFunction(post)) {
callback = post;
post = null;
}
if (method == 'GET') {
var data, dataCached;
if (dataCached = cache.data[url]) {
$defer(function() { callback(200, copy(dataCached.value)); });

if (sync) {
callback(200, copy(dataCached.value));
} else {
$defer(function() { callback(200, copy(dataCached.value)); });
}

if (!verifyCache)
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,12 @@ angularWidget('ng:include', function(element){
useScope = this.$eval(scopeExp);

if (src) {
xhr('GET', src, function(code, response){
xhr('GET', src, null, function(code, response){
element.html(response);
childScope = useScope || createScope(scope);
compiler.compile(element)(childScope);
scope.$eval(onloadExp);
});
}, false, true);
} else {
childScope = null;
element.html('');
Expand Down Expand Up @@ -1066,10 +1066,10 @@ angularWidget('ng:view', function(element) {
}

if (src) {
$xhr('GET', src, function(code, response){
$xhr('GET', src, null, function(code, response){
element.html(response);
compiler.compile(element)(childScope);
});
}, false, true);
} else {
element.html('');
}
Expand Down
16 changes: 16 additions & 0 deletions test/service/xhr.cacheSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ describe('$xhr.cache', function() {
});


it('should call callback synchronously when sync flag is on', function() {
$browserXhr.expectGET('/url').respond('+');
cache('GET', '/url', null, callback, false, true);
expect(log).toEqual(''); //callback hasn't executed

$browserXhr.flush();
expect(log).toEqual('"+";'); //callback has executed

cache('GET', '/url', null, callback, false, true);
expect(log).toEqual('"+";"+";'); //callback has executed

$browser.defer.flush();
expect(log).toEqual('"+";"+";'); //callback was not called again any more
});


it('should call eval after callbacks for both cache hit and cache miss execute', function() {
var eval = this.spyOn(scope, '$eval').andCallThrough();

Expand Down
4 changes: 0 additions & 4 deletions test/widgetsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ describe("widget", function(){
scope.url = 'myUrl';
scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'};
scope.$eval();
scope.$service('$browser').defer.flush();
expect(element.text()).toEqual('misko');
dealoc(scope);
});
Expand All @@ -623,7 +622,6 @@ describe("widget", function(){
scope.url = 'myUrl';
scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'};
scope.$eval();
scope.$service('$browser').defer.flush();

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

Expand All @@ -640,7 +638,6 @@ describe("widget", function(){
scope.url = 'myUrl';
scope.$service('$xhr.cache').data.myUrl = {value:'{{c=c+1}}'};
scope.$eval();
scope.$service('$browser').defer.flush();

// this one should really be just '1', but due to lack of real events things are not working
// properly. see discussion at: http://is.gd/ighKk
Expand All @@ -657,7 +654,6 @@ describe("widget", function(){
scope.url = 'myUrl';
scope.$service('$xhr.cache').data.myUrl = {value:'my partial'};
scope.$eval();
scope.$service('$browser').defer.flush();
expect(element.text()).toEqual('my partial');
expect(scope.loaded).toBe(true);
dealoc(element);
Expand Down