Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

While True binding? Update On bindings? #8543

Closed

Description

@cesarandreu

I'm not sure if this has been brought up before; I couldn't find any prior discussion. Maybe someone can point me in the correct way.

We now have one-time binding with {{ ::foo }}.

Another use-case I'd consider very useful is a binding that will continue updating while a certain expression is true, and then become unbound once it returns false.

For example:

// Controller
$scope.Job = {
  status: 'running',
  isActive: function () {
    return ['finished', 'error'].indexOf(this.status) === -1;
  }
};
<!-- View -->
<div>
  {{ Job.isActive()::Job.status }}
</div>

In this case, once Job.status has been update to finished or error, it can remove the watcher because Job.isActive() will return false, so it knows that it will no longer continue changing.

I have a lot of bindings in which I only want temporary bindings, this would help to reduce the number of watchers.

Would there be any interest in having some sort of concept of manually triggered bindings? For a lot of my data sets I'll usually know when they're changing (for example, if it's data coming from the server that I'm refreshing), so instead of having to check them in every cycle, I could manually trigger when they've changed. It's not great, and it's probably not that useful now that there's Object.observe; has it been discussed? I've considered implementing a directive to sorta do this for me, by registering listeners and emitting events through a service or something along those lines.

Activity

  1. caitp commented on Aug 8, 2014

    @caitp
    Contributor

    so, there has been some talk about supporting groups of watches which can be enabled/disabled imperatively. It's been thrown away for now, but might come back in the future (we do want it).

    This, however, is essentially "watch this one expression, and if it's truthy, also watch this other expression", which seems kinda weird, and not at all a performance enhancement

  2. cesarandreu commented on Aug 8, 2014

    @cesarandreu
    Author

    Maybe I wasn't clear, I was thinking: watch Job.status while Job.isActive() is truthy. Once Job.isActive() stops being truthy, remove both watches. Even if Job.isActive() were to become truthy later, it'll no longer be watched.

    Basically, stop watching once a certain condition is met, because I know it's no longer going to get updated.

  3. btford commented on Aug 18, 2014

    @btford
    Contributor

    Can't you do this with {{ ::Job.isActive() && Job.status }} ?

  4. added this to the milestone on Aug 18, 2014
  5. caitp commented on Aug 18, 2014

    @caitp
    Contributor

    only if Job.isActive() returns undefined until it's true, which seems unlikely unless the code is specifically designed for this

  6. Narretz commented on Apr 27, 2017

    @Narretz
    Contributor

    It should be possible to achieve this with suspending watchers on the scope, which is still under consideration: #10658

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions