Skip to content

Commit

Permalink
clear dataOption on a non-selection change.
Browse files Browse the repository at this point in the history
  • Loading branch information
rniemeyer committed Dec 20, 2013
1 parent cca0622 commit b56374e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.2.0",
"version": "0.2.1",
"main": "build/knockout-jqAutocomplete.min.js",
"ignore": [
"examples",
Expand Down
16 changes: 11 additions & 5 deletions build/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-jqAutocomplete 0.2.0 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-jqAutocomplete 0.2.1 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand Down Expand Up @@ -42,17 +42,23 @@

//handle updating the actual value
config.select = function(event, ui) {
options.value(ui.item && ui.item.actual);
if (ui.item && ui.item.actual) {
options.value(ui.item.actual);

if (ko.isWriteableObservable(options.dataValue)) {
options.dataValue(ui.item.data);
if (ko.isWriteableObservable(options.dataValue)) {
options.dataValue(ui.item.data);
}
}
};

//user made a change without selecting a value from the list
config.change = function(event, ui) {
if (!ui.item) {
if (!ui.item || !ui.item.actual) {
options.value(event.target && event.target.value);

if (ko.isWriteableObservable(options.dataValue)) {
options.dataValue(null);
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions build/knockout-jqAutocomplete.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.2.0",
"version": "0.2.1",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
Expand Down
22 changes: 20 additions & 2 deletions spec/knockout-jqAutocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,7 @@ describe("knockout-jqAutocomplete", function(){
});

it("should populate the value when not selecting from list", function() {
var $listItems,
items = ["one", "two", "three"],
var items = ["one", "two", "three"],
value = ko.observable();

ko.applyBindingsToNode(input, {
Expand All @@ -708,11 +707,30 @@ describe("knockout-jqAutocomplete", function(){
}
});

$input.val("test").blur();
$input.trigger("autocompletechange");

expect(value()).toEqual("test");
});

it("should clear the dataValue when not selecting from list", function() {
var items = ["one", "two", "three"],
value = ko.observable(),
dataValue = ko.observable("test");

ko.applyBindingsToNode(input, {
jqAuto: {
value: value,
dataValue: dataValue,
source: items
}
});

$input.val("test").blur();
$input.trigger("autocompletechange");

expect(value()).toEqual("test");
expect(dataValue()).toEqual(null);
});

it("should respect the valueProp option on selection", function() {
Expand Down
14 changes: 10 additions & 4 deletions src/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@

//handle updating the actual value
config.select = function(event, ui) {
options.value(ui.item && ui.item.actual);
if (ui.item && ui.item.actual) {
options.value(ui.item.actual);

if (ko.isWriteableObservable(options.dataValue)) {
options.dataValue(ui.item.data);
if (ko.isWriteableObservable(options.dataValue)) {
options.dataValue(ui.item.data);
}
}
};

//user made a change without selecting a value from the list
config.change = function(event, ui) {
if (!ui.item) {
if (!ui.item || !ui.item.actual) {
options.value(event.target && event.target.value);

if (ko.isWriteableObservable(options.dataValue)) {
options.dataValue(null);
}
}
};

Expand Down

0 comments on commit b56374e

Please sign in to comment.