mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-27 06:54:01 +00:00
resources, with bind()
This commit is contained in:
parent
f9f181a33e
commit
79b743e52f
2 changed files with 26 additions and 6 deletions
|
|
@ -43,14 +43,14 @@ ResourceFactory.DEFAULT_ACTIONS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
ResourceFactory.prototype = {
|
ResourceFactory.prototype = {
|
||||||
route: function(url, idPaths, actions){
|
route: function(url, paramDefaults, actions){
|
||||||
var self = this;
|
var self = this;
|
||||||
var route = new Route(url);
|
var route = new Route(url);
|
||||||
actions = $.extend({}, ResourceFactory.DEFAULT_ACTIONS, actions);
|
actions = $.extend({}, ResourceFactory.DEFAULT_ACTIONS, actions);
|
||||||
function extractIds(data){
|
function extractParams(data){
|
||||||
var ids = {};
|
var ids = {};
|
||||||
foreach(idPaths, function(path, id){
|
foreach(paramDefaults, function(value, key){
|
||||||
ids[id] = Scope.getter(data, path);
|
ids[key] = value.charAt && value.charAt(0) == '@' ? Scope.getter(data, value.substr(1)) : value;
|
||||||
});
|
});
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
@ -83,7 +83,7 @@ ResourceFactory.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
var value = action.isArray ? [] : new Resource(data);
|
var value = action.isArray ? [] : new Resource(data);
|
||||||
self.xhr.method(action.method, route.url($.extend({}, action.params || {}, extractIds(data), params)), data, function(response) {
|
self.xhr.method(action.method, route.url($.extend({}, action.params || {}, extractParams(data), params)), data, function(response) {
|
||||||
if (action.isArray) {
|
if (action.isArray) {
|
||||||
foreach(response, function(item){
|
foreach(response, function(item){
|
||||||
value.push(new Resource(item));
|
value.push(new Resource(item));
|
||||||
|
|
@ -96,6 +96,10 @@ ResourceFactory.prototype = {
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Resource.bind = function(additionalParamDefaults){
|
||||||
|
return self.route(url, $.extend({}, paramDefaults, additionalParamDefaults), actions);
|
||||||
|
};
|
||||||
|
|
||||||
if (!isGet) {
|
if (!isGet) {
|
||||||
Resource.prototype['$' + name] = function(a1, a2){
|
Resource.prototype['$' + name] = function(a1, a2){
|
||||||
var params = {};
|
var params = {};
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ describe("resource", function() {
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
xhr = new MockXHR();
|
xhr = new MockXHR();
|
||||||
resource = new ResourceFactory(xhr);
|
resource = new ResourceFactory(xhr);
|
||||||
CreditCard = resource.route('/CreditCard/:id:verb', {id:'id.key'}, {
|
CreditCard = resource.route('/CreditCard/:id:verb', {id:'@id.key'}, {
|
||||||
charge:{
|
charge:{
|
||||||
method:'POST',
|
method:'POST',
|
||||||
params:{verb:'!charge'}
|
params:{verb:'!charge'}
|
||||||
|
|
@ -80,6 +80,15 @@ describe("resource", function() {
|
||||||
expect(typeof CreditCard.query).toBe('function');
|
expect(typeof CreditCard.query).toBe('function');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should build resource with default param", function(){
|
||||||
|
xhr.expectGET('/Order/123/Line/456.visa?minimum=0.05').respond({id:'abc'});
|
||||||
|
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});
|
||||||
|
xhr.flush();
|
||||||
|
nakedExpect(item).toEqual({id:'abc'});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
it("should create resource", function(){
|
it("should create resource", function(){
|
||||||
xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'});
|
xhr.expectPOST('/CreditCard').data({name:'misko'}).respond({id:123, name:'misko'});
|
||||||
|
|
||||||
|
|
@ -155,5 +164,12 @@ describe("resource", function() {
|
||||||
expect(callback).wasCalledWith(cc);
|
expect(callback).wasCalledWith(cc);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should bind default parameters', function(){
|
||||||
|
xhr.expectGET('/CreditCard/123.visa?minimum=0.05').respond({id:123});
|
||||||
|
var Visa = CreditCard.bind({verb:'.visa', minimum:0.05});
|
||||||
|
var visa = Visa.get({id:123});
|
||||||
|
xhr.flush();
|
||||||
|
nakedExpect(visa).toEqual({id:123});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue