Skip to content

Commit

Permalink
v0.4.2 - ensure that when an observable value becomes empty that the …
Browse files Browse the repository at this point in the history
…field can be cleared
  • Loading branch information
rniemeyer committed Apr 15, 2015
1 parent a272b43 commit a0c04f9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
reports/
bower_components
bower_components
.idea
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.4.0",
"version": "0.4.2",
"main": "build/knockout-jqAutocomplete.min.js",
"ignore": [
"examples",
Expand All @@ -16,4 +16,4 @@
"knockout.js": "~3.0.0",
"jqueryui": "~1.10.3"
}
}
}
37 changes: 20 additions & 17 deletions build/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// knockout-jqAutocomplete 0.4.1 | (c) 2015 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
// knockout-jqAutocomplete 0.4.2 | (c) 2015 Ryan Niemeyer | http://www.opensource.org/licenses/mit-license
;(function(factory) {
if (typeof define === "function" && define.amd) {
// AMD anonymous module
Expand Down Expand Up @@ -98,24 +98,27 @@
options = unwrap(valueAccessor()),
value = unwrap(options && options.value);

if (!value && value !== 0) {
value = "";
}

// find the appropriate value for the input
if (value || value === 0) {
sources = unwrap(options.source);
propNames = self.getPropertyNames(valueAccessor);

// if there is local data, then try to determine the appropriate value for the input
if ($.isArray(sources) && propNames.value) {
value = ko.utils.arrayFirst(sources, function (opt) {
return opt[propNames.value] == value; }
) || value;
}
sources = unwrap(options.source);
propNames = self.getPropertyNames(valueAccessor);

if (propNames.input && value && typeof value === "object") {
element.value = value[propNames.input];
}
else {
element.value = value;
}
// if there is local data, then try to determine the appropriate value for the input
if ($.isArray(sources) && propNames.value) {
value = ko.utils.arrayFirst(sources, function (opt) {
return opt[propNames.value] == value;
}
) || value;
}

if (propNames.input && value && typeof value === "object") {
element.value = value[propNames.input];
}
else {
element.value = value;
}
};

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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knockout-jqAutocomplete",
"version": "0.4.1",
"version": "0.4.2",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-uglify": "0.x.x",
Expand All @@ -11,4 +11,4 @@
"grunt-template-jasmine-istanbul": "0.x.x",
"grunt-bower-task": "~0.3.4"
}
}
}
32 changes: 31 additions & 1 deletion spec/knockout-jqAutocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,36 @@ describe("knockout-jqAutocomplete", function(){
expect(input.value).toEqual("test");
});

it("should clear the input's value, when value is null", function() {
instance.update(input, function() {
return {
value: ko.observable(null)
};
});

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

it("should clear the input's value, when value is undefined", function() {
instance.update(input, function() {
return {
value: ko.observable()
};
});

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

it("should clear the input's value, when value is an empty string", function() {
instance.update(input, function() {
return {
value: ko.observable()
};
});

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

it("should set the input's value to labelProp based on valueProp", function () {
var items = [{ id: "1", name: "One" }],
value = ko.observable("1");
Expand Down Expand Up @@ -1018,4 +1048,4 @@ describe("knockout-jqAutocomplete", function(){
expect("item" in handler.calls.argsFor(0)[1]).toBeTruthy();
});
});
});
});
35 changes: 19 additions & 16 deletions src/knockout-jqAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,27 @@
options = unwrap(valueAccessor()),
value = unwrap(options && options.value);

if (!value && value !== 0) {
value = "";
}

// find the appropriate value for the input
if (value || value === 0) {
sources = unwrap(options.source);
propNames = self.getPropertyNames(valueAccessor);

// if there is local data, then try to determine the appropriate value for the input
if ($.isArray(sources) && propNames.value) {
value = ko.utils.arrayFirst(sources, function (opt) {
return opt[propNames.value] == value; }
) || value;
}
sources = unwrap(options.source);
propNames = self.getPropertyNames(valueAccessor);

if (propNames.input && value && typeof value === "object") {
element.value = value[propNames.input];
}
else {
element.value = value;
}
// if there is local data, then try to determine the appropriate value for the input
if ($.isArray(sources) && propNames.value) {
value = ko.utils.arrayFirst(sources, function (opt) {
return opt[propNames.value] == value;
}
) || value;
}

if (propNames.input && value && typeof value === "object") {
element.value = value[propNames.input];
}
else {
element.value = value;
}
};

Expand Down

0 comments on commit a0c04f9

Please sign in to comment.