-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Add option to $watch to prevent recursive firing #834
Comments
Yep, if you change watched object from the watcher, it will always lead to infinite digest. That feels a bit like a dog trying to catch his own tail :-D How about this http://jsfiddle.net/vojtajina/una6x/27/ ? |
Actually Igor solution proved to be better for me since I have a complex object. NY reason for posting is not because I need a fix anymore but because I think this solution should be in the core. Sent via Hubroid |
This is stale. If you think it's worth discussing more, let me know and I'd be happy to reopen it. |
Is a solution for this available yet? I really think it's worth the discussion. I need to watch an array of objects and perform async tasks to some of the objects, so boolean flags will not work with my indeterministic work flow. I think being able to modify the watched object inside the callback would be an awesome feature. |
Igor's link above doesn't work. |
I would be happy to see such a feature too. |
👍 |
I worked out a different solution. If my watcher functions makes changes to the object it is watching I will tell the watcher the new value before it is triggered. This solves the recursion problem. |
I'd like to have this as well, as I'm trying to implement an undo function that can call itself recursively. |
+1 |
+1 for this... It's just drove me mad... |
+1 |
i've worked out another workaround for the issue: http://jsfiddle.net/ubdr3ou5/ |
+1. This would solve many problems :). If you want to do dom manipulations using directives, often you want the directive to listen to a model update and then put it to 'default' value after doing your stuff inside the $watch method. But the moment I change the model to its 'default' value, it triggers the $watch again. |
+1 |
1 similar comment
+1 |
+1 Meanwhile, @schellmax 's solution is the most elegant one. |
+1 |
1 similar comment
+1 |
Please do not only add @btford, would you guys be willing to please reopen this? I'm not really a fan of |
@yaminm, please see previous comment re |
+1 |
2 similar comments
+1 |
+1 |
We believe that one of |
When watching an object, if I make changes to the object from within the callback, this will trigger the $watch to be fired again after it's completion. This can occasionally cause recursive loops or redundant firing of the same $watch un-necessarily. It would be nice if we could pass an option (or imploy it by default) a way to ignore this. Other $watches can be fired, but the same $watch should not fire itself from it's own changes.
Example Use Case:
I have a complex reports & pagination page containing a lot of different filters
![Filters](https://camo.githubusercontent.com/13fd016f744f3a000f6227d48426777ec53d3c201c24834a5f7267349097c4e2/687474703a2f2f662e636c2e6c792f6974656d732f315832363241304f336e3431316e314c314331742f53637265656e25323053686f74253230323031322d30332d32382532306174253230332e31382e3030253230504d2e706e67)
(cropped for privacy)
The filters are on the left and the result table is on the right.
Any time one of the filters changes, i need to fire an ajax request to immediately update the results table. I could watch all 10-20 of my individual filters, including the non-visible flags that might be associated with them (such as paging, limit, etc) but it's much easier to lump them all into 1 object and simply change the object to refresh the page.
For instance, if i want page 2, i simply do
filters.page++
The problem is that some filters involve a 2 step process like selecting 1 filter might enable or render a second optional filter. Or perhaps certain options might select or deselect other filters. This actually seems like a fairly common scenario for moderately complex pages.
@IgorMinar provided this solution http://jsfiddle.net/IgorMinar/pn6Ur/1/ which still fires sibling $watches but does not allow a $watch to re-fire itself due to its own changes. I tried to think of any scenario where I would prefer the alternative behavior but I could only conclude that this is a much easier scenario to deal with.
I would like to propose adding it to the core. If there is a concern with overhead, perhaps make it an option for
$watch
at the very leastThe text was updated successfully, but these errors were encountered: