Skip to content

Commit

Permalink
[Close #10] Fix where, findWhere, and similar by unwrapping observabl…
Browse files Browse the repository at this point in the history
…es in matching functions
  • Loading branch information
kamranayub committed Mar 13, 2016
1 parent b2e5ccb commit f58591b
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 5 deletions.
27 changes: 26 additions & 1 deletion build/underscore-ko.js

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

2 changes: 1 addition & 1 deletion build/underscore-ko.js.map

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

4 changes: 2 additions & 2 deletions build/underscore-ko.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
@@ -1,7 +1,7 @@
{
"name": "underscore-ko",
"description": "Dead simple utility that attaches Underscore.js array and collection functions (and mutators) to Knockout.js observable arrays.",
"version": "1.8.0",
"version": "1.8.1",
"author": "Kamran Ayub <http://kamranicus.com>",
"homepage": "https://github.com/kamranayub/UnderscoreKO",
"repository": {
Expand Down
16 changes: 16 additions & 0 deletions spec/spec.js
Expand Up @@ -151,9 +151,25 @@ describe("UnderscoreKO", function () {
expect(vm.objs.where({b:1})).toEqual([vm.objs()[0],vm.objs()[2]]);
});

it("supports where with observable values", function () {
var obsa = ko.observable({ b: 1 }),
obsb = ko.observable({ b: 0 }),
arr = ko.observableArray([obsa, obsb]);

expect(arr.where({b:1})).toEqual([obsa]);
});

it("supports findWhere", function () {
expect(vm.objs.findWhere({b:1})).toEqual(vm.objs()[0]);
});

it("supports findWhere with observable values", function () {
var obsa = ko.observable({ b: 1 }),
obsb = ko.observable({ b: 0 }),
arr = ko.observableArray([obsa, obsb]);

expect(arr.findWhere({b:1})).toEqual(obsa);
});

it("supports invoke", function () {
expect(vm.arr.invoke("toString")).toEqual(["0", "1", "2", "3", "4"]);
Expand Down
26 changes: 26 additions & 0 deletions src/underscore-ko.ts
Expand Up @@ -66,6 +66,32 @@
"intersection", "intersection_",
"zip", "zip_"
];

// replaces _.property with support for KO observable values
_.property = function(key) {
return function(obj) {
return obj == null ? void 0 : ko.utils.unwrapObservable(obj)[key];
};
};

// replaces _.propertyOf with support for KO observable values
_.propertyOf = function(obj) {
return obj == null ? function(){} : function(key) {
return ko.utils.unwrapObservable(obj)[key];
};
};

// replaces _.isMatch with support for KO observable values
_.isMatch = function(object, attrs) {
var keys = _.keys(attrs), length = keys.length;
if (object == null) return !length;
var obj = Object(ko.utils.unwrapObservable(object));
for (var i = 0; i < length; i++) {
var key = keys[i];
if (attrs[key] !== obj[key] || !(key in obj)) return false;
}
return true;
};

_.each(_.union(methods, arrayMethods), function (fn) {
// Let's be good and safe Javascript citizens!
Expand Down

0 comments on commit f58591b

Please sign in to comment.