Permalink
Browse files

api.data(obj) method for setting api-wide data

  • Loading branch information...
1 parent 601c0d8 commit ff492500774fa681ffdfaf5733ec2e1494d66585 @mourner mourner committed Aug 15, 2011
Showing with 27 additions and 13 deletions.
  1. +7 −2 crude.js
  2. +20 −11 spec/spec.js
View
@@ -28,7 +28,7 @@
Crude.api = function (baseUrl, format, requestFn) {
return new Crude.Api(baseUrl, format, requestFn);
};
-
+
// the root class of the API (handles basic configuration)
@@ -54,7 +54,7 @@
method = 'get';
}
- data = data || {};
+ data = Crude.extend({}, data, this.baseData);
var url = this.baseUrl + '/' + path + '.' + this.format;
@@ -63,6 +63,11 @@
return this.requestFn(url, method, data);
},
+
+ // set api-wide request data
+ data: function (data) {
+ this.baseData = data;
+ },
// example: api.resources('post') creates Crude.Resources instance as api.posts
resources: function (name, pluralName) {
View
@@ -10,20 +10,20 @@ describe("Crude", function() {
describe('request', function() {
it('should use the config passed in the Crude.api function correctly', function() {
- var data = {},
+ var data = {foo: 'bar'},
result = api.request('test', 'put', data);
expect(result.url).toEqual('http://example.com/test.js');
expect(result.method).toEqual('put');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('should work with two arguments, using "get" as the default method', function() {
- var data = {},
+ var data = {foo: 'bar'},
result = api.request('test', data);
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('should evaluate each {prop} in the url with corresponding property from the data, also removing the prop from the latter', function() {
@@ -38,6 +38,15 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/test/1/bla/3.js');
expect(result.data).toEqual({bar: 2});
});
+
+ it('should add data set through api.data(data) to requests globally', function() {
+ api.data({apikey: 'qwerty'});
+
+ var data = {foo: 'bar'},
+ result = api.request('test', data);
+
+ expect(result.data).toEqual({foo: 'bar', apikey: 'qwerty'});
+ });
});
describe('resources', function() {
@@ -68,15 +77,15 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/posts.js');
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('should have get(id) working correctly', function() {
var result = api.posts.get(1, data);
expect(result.url).toEqual('http://example.com/posts/1.js');
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('should have create(props) working correctly', function() {
@@ -108,7 +117,7 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/posts/1.js');
expect(result.method).toEqual('delete');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('memberAction(name, opts) should create corresponding method properly', function() {
@@ -118,7 +127,7 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/posts/1/custom.js');
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('memberAction options should work properly', function() {
@@ -146,7 +155,7 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/posts/custom.js');
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('collectionAction options should work properly', function() {
@@ -204,7 +213,7 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/posts/1/comments/2/foo.js');
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
it('api-wide memberAction on nested resources through api.nestedResources should work correctly', function() {
@@ -214,7 +223,7 @@ describe("Crude", function() {
expect(result.url).toEqual('http://example.com/posts/1/comments/2/bar.js');
expect(result.method).toEqual('get');
- expect(result.data).toBe(data);
+ expect(result.data).toEqual(data);
});
});
});

0 comments on commit ff49250

Please sign in to comment.