Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_.where doesn't work when items in array are observable #10

Closed
kamranayub opened this issue Jun 10, 2013 · 6 comments
Closed

_.where doesn't work when items in array are observable #10

kamranayub opened this issue Jun 10, 2013 · 6 comments
Labels

Comments

@kamranayub
Copy link
Owner

From Google Groups:

The following code is from underscore.js this code never finds a match b/c value[key] is an observable object. If I change the code to value[key]() then the where works as you'd expect.

_.where = function(obj, attrs, first) {
    if (_.isEmpty(attrs)) return first ? void 0 : [];
    return _[first ? 'find' : 'filter'](obj, function(value) {
      for (var key in attrs) {
        if (attrs[key] !== value[key]) return false;
      }
      return true;
    });

UKO should make this compatible by using ko.util.unwrapObservable.

@Nesse
Copy link

Nesse commented Jul 16, 2013

Hey kamranayub.

I love your library, but was struggling with the where statement until i found this issue. Is this bug fixed in the latest build? I don't want to change the underscore library because i use nuget to pull the latest updates, and will forget about this. Is there another method achieving this:

<ol data-bind="template: { name: 'nested-template', foreach: Categories.where({ ParentCategoryId: null }) }" class="dd-list">
</ol>

@kamranayub
Copy link
Owner Author

I'd like to find a way while avoiding duplicating the Underscore code, but worse comes to worse, you can just declare this after loading Underscore and my lib:

ko.observableArray.fn.where = function(obj, attrs, first) {
    if (_.isEmpty(attrs)) return first ? void 0 : [];
    return _[first ? 'find' : 'filter'](obj, function(value) {
      for (var key in attrs) {
        if (attrs[key] !== ko.util.unwrapObservable(value[key])) return false;
      }
      return true;
    });

@Nesse
Copy link

Nesse commented Jul 16, 2013

Thx for your response. Can i use your code in the html with knockout like this?

<ol data-bind="template: { name: 'nested-template', foreach: Categories.where({ ParentCategoryId: null }) }" class="dd-list">
</ol>

@kamranayub
Copy link
Owner Author

Yes, it should work

@kamranayub
Copy link
Owner Author

The newest version resolves this issue :)

@kamranayub
Copy link
Owner Author

Just pushed a fix that also supports arrays where the items are view models themselves and contain observables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants