0

This seems like a straightforward implementation:

ko.observableArray.fn.findEl = function(id) {
    return ko.computed(function() {
      var ary = this();
      return _.any(ary, function(user) { return user.id() === id;});
    }, this);
};

But when I call it like:

user.current.following.findEl(valueAccessor())

It returns a function, requiring me to call findEl like:

user.current.following.findEl(valueAccessor())()

which is needless to say, not ideal.

2 Answers 2

1

Right, you're returning the actual computed observable function, which I'm sure you're aware. As it stands I'd suggest calling the value accessor of the computed observable you're returning inside your findEl function and just return that value. That at least keeps it inside your one function and you don't have to worry about having to call the function with the ()() every time.

Sign up to request clarification or add additional context in comments.

Comments

1

This works only if you use it to define a new observable in your view model:

function User() {
    this.following = ko.observableArray([]);
    this.el = this.following.findEl(valueAccessor());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.