Skip to content

Commit

Permalink
Add a simple fix for emptied query string
Browse files Browse the repository at this point in the history
  • Loading branch information
akovalyov committed Feb 8, 2016
1 parent 3148c1b commit 24931c9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 9 additions & 1 deletion chosen.ajaxaddition.jquery.js
Expand Up @@ -132,7 +132,15 @@
keyRight = $.Event('keyup');
keyRight.which = 39;
//highlight
input.val(!inputEmptied ? data.q : inputEmptiedValue).trigger(keyRight).get(0).style.background = inputBG;
var val = null;
if(!inputEmptied){
val = typeof data.q === 'undefined' ? inputEmptiedValue : data.q
}
else{
val = inputEmptiedValue;
}

input.val(val).trigger(keyRight).get(0).style.background = inputBG;

if (items.length > 0) {
$('.no-results', chosen).hide();
Expand Down
29 changes: 27 additions & 2 deletions test/main.js
Expand Up @@ -98,6 +98,31 @@ describe('chosen.ajaxaddition', function(){
server.restore();
clock.restore();
});

it("should keep the search term if nothing was passed from server", function() {
var chosen,
input,
server = sinon.fakeServer.create(),
clock = sinon.useFakeTimers();
server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' }, '{ results: [{ id: 1, text: "porshe" }] }']
);
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url:'/search'
},{}).next();
chosen.trigger('click');
clock.tick(50);//AbstractChosen.prototype.input_focus -> fires focus logic after 50
input = $('input', chosen).val('por');
server.respond();
expect(input.val()).to.equal('por');

server.restore();
clock.restore();
});

it("should not fire ajax if value was pasted without focused input field", function() {
var chosen,
input,
Expand Down Expand Up @@ -1276,7 +1301,7 @@ describe('chosen.ajaxaddition', function(){
[200, { 'Content-Type': 'application/json' },
'{ "q": "La", "results": [{"id": 2, "text": "Lamborgini"}] }']
);

$('input', chosen).trigger('click');
this.clock.tick(50);//AbstractChosen.prototype.input_focus -> fires focus logic after 50
input.val('L');
Expand All @@ -1291,7 +1316,7 @@ describe('chosen.ajaxaddition', function(){
input.val('');
input.trigger(key);
this.server.respond();

expect(input.val()).to.be.empty;

//verify that it still has options selected
Expand Down

0 comments on commit 24931c9

Please sign in to comment.