Skip to content

Commit b56374e

Browse files
committed
clear dataOption on a non-selection change.
1 parent cca0622 commit b56374e

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "knockout-jqAutocomplete",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "build/knockout-jqAutocomplete.min.js",
55
"ignore": [
66
"examples",

build/knockout-jqAutocomplete.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// knockout-jqAutocomplete 0.2.0 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
1+
// knockout-jqAutocomplete 0.2.1 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
22
;(function(factory) {
33
if (typeof define === "function" && define.amd) {
44
// AMD anonymous module
@@ -42,17 +42,23 @@
4242

4343
//handle updating the actual value
4444
config.select = function(event, ui) {
45-
options.value(ui.item && ui.item.actual);
45+
if (ui.item && ui.item.actual) {
46+
options.value(ui.item.actual);
4647

47-
if (ko.isWriteableObservable(options.dataValue)) {
48-
options.dataValue(ui.item.data);
48+
if (ko.isWriteableObservable(options.dataValue)) {
49+
options.dataValue(ui.item.data);
50+
}
4951
}
5052
};
5153

5254
//user made a change without selecting a value from the list
5355
config.change = function(event, ui) {
54-
if (!ui.item) {
56+
if (!ui.item || !ui.item.actual) {
5557
options.value(event.target && event.target.value);
58+
59+
if (ko.isWriteableObservable(options.dataValue)) {
60+
options.dataValue(null);
61+
}
5662
}
5763
};
5864

build/knockout-jqAutocomplete.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "knockout-jqAutocomplete",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"devDependencies": {
55
"grunt": "~0.4.1",
66
"grunt-contrib-uglify": "0.x.x",

spec/knockout-jqAutocomplete.spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,7 @@ describe("knockout-jqAutocomplete", function(){
697697
});
698698

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

704703
ko.applyBindingsToNode(input, {
@@ -708,11 +707,30 @@ describe("knockout-jqAutocomplete", function(){
708707
}
709708
});
710709

710+
$input.val("test").blur();
711+
$input.trigger("autocompletechange");
712+
713+
expect(value()).toEqual("test");
714+
});
715+
716+
it("should clear the dataValue when not selecting from list", function() {
717+
var items = ["one", "two", "three"],
718+
value = ko.observable(),
719+
dataValue = ko.observable("test");
720+
721+
ko.applyBindingsToNode(input, {
722+
jqAuto: {
723+
value: value,
724+
dataValue: dataValue,
725+
source: items
726+
}
727+
});
711728

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

715732
expect(value()).toEqual("test");
733+
expect(dataValue()).toEqual(null);
716734
});
717735

718736
it("should respect the valueProp option on selection", function() {

src/knockout-jqAutocomplete.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,23 @@
4141

4242
//handle updating the actual value
4343
config.select = function(event, ui) {
44-
options.value(ui.item && ui.item.actual);
44+
if (ui.item && ui.item.actual) {
45+
options.value(ui.item.actual);
4546

46-
if (ko.isWriteableObservable(options.dataValue)) {
47-
options.dataValue(ui.item.data);
47+
if (ko.isWriteableObservable(options.dataValue)) {
48+
options.dataValue(ui.item.data);
49+
}
4850
}
4951
};
5052

5153
//user made a change without selecting a value from the list
5254
config.change = function(event, ui) {
53-
if (!ui.item) {
55+
if (!ui.item || !ui.item.actual) {
5456
options.value(event.target && event.target.value);
57+
58+
if (ko.isWriteableObservable(options.dataValue)) {
59+
options.dataValue(null);
60+
}
5561
}
5662
};
5763

0 commit comments

Comments
 (0)