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

parametrical computed / recursive observation (make methods observable) #147

Closed
davismj opened this issue Aug 11, 2015 · 16 comments
Closed

Comments

@davismj
Copy link
Member

davismj commented Aug 11, 2015

In knockout, I would bind to a function that computes a value and returns a string. The value I passed in would largely be static, like an index on an array, but the function would then look up observables to compute the final value and thus set up subscriptions on those values.

In Aurelia, there is nothing like this. I cannot make a function called computeValueForIndex(idx) that subscribes to (or even dirty checks!) other variables on the view model. I cannot use a a computed get computeValueForIndex(idx) because getters cannot accept parameters.

@jdanyow
Copy link
Contributor

jdanyow commented Aug 11, 2015

Is this the scenario:

ko view:

<span data-bind="text: computeValueForIndex(idx)"></span>

ko view-model:

// when ko invokes this function it makes note of the two observables that are called and will update bindings using this function when either of the two observables change
computeValueForIndex(idx) {
  return this.someObservable()[idx] + this.otherObservable();
}

In other words, we need the equivalent of @computedFrom or an aurelia-computed for methods on the viewmodel (only properties are supported today).

@davismj
Copy link
Member Author

davismj commented Aug 11, 2015

precisely

@jdanyow
Copy link
Contributor

jdanyow commented Dec 28, 2015

todo: consider when working on #149 and #249

@EisenbergEffect
Copy link
Contributor

@jdanyow Should we consider rolling in your computed plugin? and somehow sharing code with that to address this and other related issues?

@davismj
Copy link
Member Author

davismj commented Dec 28, 2015

Why not into core?

@jdanyow jdanyow changed the title parametrical computed / recursive observation parametrical computed / recursive observation (make methods observable) Feb 9, 2016
@jods4
Copy link
Contributor

jods4 commented Feb 15, 2016

Something I've been musing is adding KO auto-detect as an optional observation mecanism into Aurelia.

I've been thinking about something could work like that (naming could be better):

class ViewModel {
  // This turns x into a property with instrumented getter, 
  // so that read access are detected
  @dependency 
  x: number;

  // can be a property or a method, or any piece of code in fact.
  // autoDependencies means that during evaluation, @dependency reads
  // should be collected and turned into dependencies, 
  // as if they had been added with @computedFrom
  @autoDependencies
  computed() {
    return this.x;
  }
}

This is basically what KO does. It could be an interesting alternative to existing mechanisms (ideally provided as an optional module).

A nice API could allow:

  • detecting and returning the dependencies of any piece of code, such as
let [result, dependencies] = AutoDependency.Evaluate(func)
  • choose whether dependencies are re-computed at each evaluation or only the first one.

@EisenbergEffect
Copy link
Contributor

What was the status on this? Can we support computedFrom on methods?

@jdanyow
Copy link
Contributor

jdanyow commented May 16, 2016

I think we'd need to add logic to CallScope and CallMember's connect method to do something like this. Not sure if we need to tackle for V1 so was leaving this in the backlog.

@EisenbergEffect
Copy link
Contributor

Sounds good. It can certainly be in a point release.

@tonypee
Copy link

tonypee commented Jul 9, 2016

This is exactly what i need! I try to do this all of the time, and need to keep putting more dirty code into the template - is there a status update on this?

@davismj
Copy link
Member Author

davismj commented Jul 9, 2016

For those reading, I would say the current best practice for this use case is to use a binding signaler. See the gist here: https://gist.run/?id=d6c534d7b0a1b26fc3f27366b0e5b7a2

@sormy
Copy link

sormy commented Oct 3, 2016

The bad part about binding signalers is that all signalers should be unique, there is no way to link signalers to specific component instance :-(

@massimocode
Copy link

@davismj Hey, I was wondering if there's any update on this? Or is the best practice still as per the gist posted above?

@glen-84
Copy link

glen-84 commented Sep 19, 2017

A binding signaler only works if you have some kind of "event" (like a click). That's not the same as just updating the binding when observable values change.

@davismj
Copy link
Member Author

davismj commented Sep 27, 2017

@massimocode Sorry I missed your mention.

@glen-84 If you pass an observable variable--that is, any variable on your view model--to a function on your view model, that will make an observable method automatically. See here:

https://gist.run/?id=0aa5bd84d78994e11e435eba811ab556

@bigopon I've solved all of my problems in this issue with the above method.

@jsobell
Copy link

jsobell commented Sep 28, 2017

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

No branches or pull requests

9 participants