Skip to content

Commit

Permalink
update value from change event, if field is changed without selecting…
Browse files Browse the repository at this point in the history
… a value
  • Loading branch information
rniemeyer committed Dec 16, 2013
1 parent be6e0d0 commit e403bb4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 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.1.2",
"version": "0.1.3",
"main": "build/knockout-jqAutocomplete.min.js",
"ignore": [
"examples",
Expand Down
9 changes: 8 additions & 1 deletion build/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-jqAutocomplete 0.1.2 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-jqAutocomplete 0.1.3 | (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 @@ -45,6 +45,13 @@
options.value(ui.item && ui.item.actual);
};

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

//initialize the widget
var widget = $(element).autocomplete(config).data("ui-autocomplete");

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.1.2",
"version": "0.1.3",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
Expand Down
19 changes: 19 additions & 0 deletions spec/knockout-jqAutocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,25 @@ describe("knockout-jqAutocomplete", function(){
expect(value()).toEqual("two");
});

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

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


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

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

it("should respect the valueProp option on selection", function() {
var $listItems,
items = [
Expand Down
7 changes: 7 additions & 0 deletions src/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
options.value(ui.item && ui.item.actual);
};

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

//initialize the widget
var widget = $(element).autocomplete(config).data("ui-autocomplete");

Expand Down

0 comments on commit e403bb4

Please sign in to comment.