Skip to content

Commit

Permalink
[#2835] Added Client#getTemplate() method
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Aug 13, 2012
1 parent a317fb9 commit 70c3d22
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ckan/public/base/javascript/client.js
Expand Up @@ -25,6 +25,36 @@
return path;
},

/* Requests a block of HTML from the snippet API endpoint. Optional
* parameters can also be provided to the template via the params
* object.
*
* filename - The filename of the snippet to load including extension.
* params - Optional query string parameters.
* success - A callback to be called on success. Receives the html string.
* error - A callback to be called on error.
*
* Examples
*
* client.getTemplate('dataset-list.html', {limit: 5}, function (html) {
* // Do something with the html.
* });
*
* Returns a jqXHR promise object.
*/
getTemplate: function (filename, params, success, error) {
var url = this.url('/api/1/util/snippet/' + encodeURIComponent(filename));

// Allow function to be called without params argument.
if (typeof params === 'function') {
error = success;
success = params;
params = {};
}

return jQuery.get(url, params || {}).then(success, error);
},

/* Fetches the current locale translation from the API.
*
* locale - The current page locale.
Expand Down
30 changes: 30 additions & 0 deletions ckan/public/base/test/spec/client.spec.js
Expand Up @@ -39,6 +39,36 @@ describe('ckan.Client()', function () {
});
});

describe('.getTemplate(filename, params, success, error)', function () {
beforeEach(function () {
this.fakePromise = sinon.stub(jQuery.Deferred());
this.fakePromise.then.returns(this.fakePromise);
sinon.stub(jQuery, 'get').returns(this.fakePromise);
});

afterEach(function () {
jQuery.get.restore();
});

it('should return a jQuery promise', function () {
var target = this.client.getTemplate('test.html');
assert.ok(target === this.fakePromise, 'target === this.fakePromise');
});

it('should request the template file', function () {
var target = this.client.getTemplate('test.html');
assert.called(jQuery.get);
assert.calledWith(jQuery.get, '/api/1/util/snippet/test.html', {});
});

it('should request the template file with any provided params', function () {
var options = {limit: 5, page: 2};
var target = this.client.getTemplate('test.html', options);
assert.called(jQuery.get);
assert.calledWith(jQuery.get, '/api/1/util/snippet/test.html', options);
});
});

describe('.getLocaleData(locale, success, error)', function () {
beforeEach(function () {
this.fakePromise = sinon.stub(jQuery.Deferred());
Expand Down

0 comments on commit 70c3d22

Please sign in to comment.