Skip to content

Commit cca0622

Browse files
committed
add dataValue option to populate with the item in addition to the value option
1 parent e403bb4 commit cca0622

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ knockout-jqAutocomplete
1414

1515
* **valueProp** - If specified, this is a property name to use to populate the value when a selection is made. If not specified, the actual item itself will be used as the value.
1616

17+
* **dataValue** - If specified, this observable will be populated with the currently selected data item. This option will allow you to populate **value** with the **valueProp** and have access to the selected object at the same time through **dataValue**.
18+
1719
* **template** - If specified, this is the name of a template to use when building each item in the menu choices. This allows full customization of the options shown to the user.
1820

1921
* **options** - Anything passed in `options` will be included as options passed when initializing the `autocomplete` widget. See http://api.jqueryui.com/autocomplete/ for a list of the available options.

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.1.3",
3+
"version": "0.2.0",
44
"main": "build/knockout-jqAutocomplete.min.js",
55
"ignore": [
66
"examples",

build/knockout-jqAutocomplete.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// knockout-jqAutocomplete 0.1.3 | (c) 2013 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
1+
// knockout-jqAutocomplete 0.2.0 | (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
@@ -43,6 +43,10 @@
4343
//handle updating the actual value
4444
config.select = function(event, ui) {
4545
options.value(ui.item && ui.item.actual);
46+
47+
if (ko.isWriteableObservable(options.dataValue)) {
48+
options.dataValue(ui.item.data);
49+
}
4650
};
4751

4852
//user made a change without selecting a value from the list

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.1.3",
3+
"version": "0.2.0",
44
"devDependencies": {
55
"grunt": "~0.4.1",
66
"grunt-contrib-uglify": "0.x.x",

spec/knockout-jqAutocomplete.spec.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,44 @@ describe("knockout-jqAutocomplete", function(){
785785
expect($input.text()).toEqual("one description");
786786
});
787787

788+
it("should respect the dataValue option on selection", function() {
789+
var $listItems,
790+
items = [
791+
{
792+
name: "one",
793+
description: "one description"
794+
},
795+
{
796+
name: "two",
797+
description: "two description"
798+
},
799+
{
800+
name: "three",
801+
description: "three description"
802+
}
803+
],
804+
value = ko.observable(),
805+
dataValue = ko.observable();
806+
807+
ko.applyBindingsToNode(input, {
808+
jqAuto: {
809+
value: value,
810+
dataValue: dataValue,
811+
source: items,
812+
valueProp: "name"
813+
}
814+
});
815+
816+
$input.autocomplete("search", "one");
817+
818+
$listItems = $("ul.ui-autocomplete li");
819+
820+
$listItems.first("a").click();
821+
822+
expect(value()).toEqual("one");
823+
expect(dataValue()).toEqual(items[0]);
824+
});
825+
788826
it("should initially set the input's value", function() {
789827
var items = [],
790828
value = ko.observable("testing");

src/knockout-jqAutocomplete.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
//handle updating the actual value
4343
config.select = function(event, ui) {
4444
options.value(ui.item && ui.item.actual);
45+
46+
if (ko.isWriteableObservable(options.dataValue)) {
47+
options.dataValue(ui.item.data);
48+
}
4549
};
4650

4751
//user made a change without selecting a value from the list

0 commit comments

Comments
 (0)