Skip to content

Commit

Permalink
[#2792] Update the formatInitialValue() method
Browse files Browse the repository at this point in the history
Select2 3.0 supports an asynchronous formatter.
  • Loading branch information
aron committed Aug 2, 2012
1 parent 2795424 commit 1a83a77
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 13 additions & 4 deletions ckan/public/base/javascript/modules/autocomplete.js
Expand Up @@ -185,18 +185,27 @@ this.ckan.module('autocomplete', function (jQuery, _) {

/* Callback function that parses the initial field value.
*
* element - The initialized input element wrapped in jQuery.
* element - The initialized input element wrapped in jQuery.
* callback - A callback to run once the formatting is complete.
*
* Returns a term object or an array depending on the type.
*/
formatInitialValue: function (element) {
formatInitialValue: function (element, callback) {
var value = jQuery.trim(element.val() || '');
var formatted;

if (this.options.tags) {
return jQuery.map(value.split(","), this.formatTerm);
formatted = jQuery.map(value.split(","), this.formatTerm);
} else {
return this.formatTerm(value);
formatted = this.formatTerm(value);
}

// Select2 v3.0 supports a callback for async calls.
if (typeof callback === 'function') {
callback(formatted);
}

return formatted;
},

/* Callback triggered when the select2 plugin needs to make a request.
Expand Down
24 changes: 19 additions & 5 deletions ckan/public/base/test/spec/modules/autocomplete.spec.js
Expand Up @@ -197,16 +197,30 @@ describe('ckan.modules.AutocompleteModule()', function () {
});
});

describe('.formatInitialValue()', function () {
it('should return an item object with id and text properties', function () {
describe('.formatInitialValue(element, callback)', function () {
beforeEach(function () {
this.callback = sinon.spy();
});

it('should pass an item object with id and text properties into the callback', function () {
var target = jQuery('<input value="test"/>');
assert.deepEqual(this.module.formatInitialValue(target), {id: 'test', text: 'test'});

this.module.formatInitialValue(target, this.callback);
assert.calledWith(this.callback, {id: 'test', text: 'test'});
});

it('should return an array of properties if options.tags is true', function () {
it('should pass an array of properties into the callback if options.tags is true', function () {
this.module.options.tags = true;
var target = jQuery('<input />', {value: "test, test"});
assert.deepEqual(this.module.formatInitialValue(target), [{id: 'test', text: 'test'}, {id: 'test', text: 'test'}]);

this.module.formatInitialValue(target, this.callback);
assert.calledWith(this.callback, [{id: 'test', text: 'test'}, {id: 'test', text: 'test'}]);
});

it('should return the value if no callback is provided (to support select2 v2.1)', function () {
var target = jQuery('<input value="test"/>');

assert.deepEqual(this.module.formatInitialValue(target), {id: 'test', text: 'test'});
});
});

Expand Down

0 comments on commit 1a83a77

Please sign in to comment.