mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-19 12:01:07 +00:00
better naming for our verify cache scheme, and tests.
This commit is contained in:
parent
8f9bf37bcf
commit
4034a2d1e2
3 changed files with 25 additions and 8 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
|
||||||
|
|
||||||
function Route(template, defaults) {
|
function Route(template, defaults) {
|
||||||
this.template = template = template + '#';
|
this.template = template = template + '#';
|
||||||
this.defaults = defaults || {};
|
this.defaults = defaults || {};
|
||||||
|
|
@ -86,7 +88,7 @@ ResourceFactory.prototype = {
|
||||||
throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments.";
|
throw "Expected between 0-3 arguments [params, data, callback], got " + arguments.length + " arguments.";
|
||||||
}
|
}
|
||||||
|
|
||||||
var value = action.isArray ? [] : new Resource(data;)
|
var value = action.isArray ? [] : new Resource(data)
|
||||||
self.xhr(
|
self.xhr(
|
||||||
action.method,
|
action.method,
|
||||||
route.url(extend({}, action.params || {}, extractParams(data), params)),
|
route.url(extend({}, action.params || {}, extractParams(data), params)),
|
||||||
|
|
@ -94,8 +96,7 @@ ResourceFactory.prototype = {
|
||||||
function(status, response, clear) {
|
function(status, response, clear) {
|
||||||
if (status == 200) {
|
if (status == 200) {
|
||||||
if (action.isArray) {
|
if (action.isArray) {
|
||||||
if (action.cacheThenRetrieve)
|
value.length = 0;
|
||||||
value = [];
|
|
||||||
foreach(response, function(item){
|
foreach(response, function(item){
|
||||||
value.push(new Resource(item));
|
value.push(new Resource(item));
|
||||||
});
|
});
|
||||||
|
|
@ -107,7 +108,7 @@ ResourceFactory.prototype = {
|
||||||
throw {status: status, response:response, message: status + ": " + response};
|
throw {status: status, response:response, message: status + ": " + response};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
action.cacheThenRetrieve
|
action.verifyCache
|
||||||
);
|
);
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -313,7 +313,7 @@ angularService('$xhr.bulk', function($xhr, $error, $log){
|
||||||
|
|
||||||
angularService('$xhr.cache', function($xhr){
|
angularService('$xhr.cache', function($xhr){
|
||||||
var inflight = {}, self = this;;
|
var inflight = {}, self = this;;
|
||||||
function cache(method, url, post, callback, cacheThenRetrieve){
|
function cache(method, url, post, callback, verifyCache){
|
||||||
if (isFunction(post)) {
|
if (isFunction(post)) {
|
||||||
callback = post;
|
callback = post;
|
||||||
post = null;
|
post = null;
|
||||||
|
|
@ -322,7 +322,7 @@ angularService('$xhr.cache', function($xhr){
|
||||||
var data;
|
var data;
|
||||||
if (data = cache.data[url]) {
|
if (data = cache.data[url]) {
|
||||||
callback(200, copy(data.value));
|
callback(200, copy(data.value));
|
||||||
if (!cacheThenRetrieve)
|
if (!verifyCache)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,7 @@ describe("resource", function() {
|
||||||
|
|
||||||
it("should build resource with default param", function(){
|
it("should build resource with default param", function(){
|
||||||
xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'});
|
xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'});
|
||||||
xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'ddd'});
|
var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05}, {verifyCache: 'blah'});
|
||||||
var LineItem = resource.route('/Order/:orderId/Line/:id:verb', {orderId: '123', id: '@id.key', verb:'.visa', minimum:0.05});
|
|
||||||
var item = LineItem.get({id:456});
|
var item = LineItem.get({id:456});
|
||||||
xhr.flush();
|
xhr.flush();
|
||||||
nakedExpect(item).toEqual({id:'abc'});
|
nakedExpect(item).toEqual({id:'abc'});
|
||||||
|
|
@ -136,6 +135,23 @@ describe("resource", function() {
|
||||||
expect(person.name).toEqual('misko');
|
expect(person.name).toEqual('misko');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return the same object when verifying the cache', function(){
|
||||||
|
var scope = angular.compile('<div></div>');
|
||||||
|
var Person = scope.$resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}});
|
||||||
|
scope.$browser.xhr.expectGET('/Person/123').respond('[\n{\nname:\n"misko"\n}\n]');
|
||||||
|
var person = Person.query({id:123});
|
||||||
|
scope.$browser.xhr.flush();
|
||||||
|
expect(person[0].name).toEqual('misko');
|
||||||
|
|
||||||
|
scope.$browser.xhr.expectGET('/Person/123').respond('[\n{\nname:\n"rob"\n}\n]');
|
||||||
|
var person2 = Person.query({id:123});
|
||||||
|
expect(person2[0].name).toEqual('misko');
|
||||||
|
var person2Cache = person2;
|
||||||
|
scope.$browser.xhr.flush();
|
||||||
|
expect(person2Cache).toEqual(person2);
|
||||||
|
expect(person2[0].name).toEqual('rob');
|
||||||
|
});
|
||||||
|
|
||||||
describe('failure mode', function(){
|
describe('failure mode', function(){
|
||||||
it('should report error when non 200', function(){
|
it('should report error when non 200', function(){
|
||||||
xhr.expectGET('/CreditCard/123').respond(500, "Server Error");
|
xhr.expectGET('/CreditCard/123').respond(500, "Server Error");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue