-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
force re-computation of a computed property #214
Comments
yeah, I can add something like |
Rethinking the problem, it's probably better to do: var vm = new Vue({
data: {
currentTime: Date.now()
},
computed: {
/* ... */
},
created: function () {
var self = this
setInterval(function () {
self.currentTime = Date.now()
}, 1000)
}
}) |
I think |
I also have an edge use case for |
Your DOM shouldn't have any sources of truth. When using Vue, the DOM should simply be a view in to your data. If you have data that only exists in the DOM you're gonna have a bad time. |
@sirlancelot Why shouldn't I have any sources of truth from DOM? Vue is an incrementally-adoptable JavaScript framework. I'd like to use Vue as a jQuery-like tool, not entrusted all DOM to Vue. I admit that Vue has a full-abstract power to let that I need not operate DOM, but sometimes, I just want to use Vue to do some simple action. |
@sirlancelot it's a bit tough to explain, but that's not I'm doing. My data/computed values remain the source of truth. To go into more detail, I'm referencing DOM elements to render an indicator element under a selected tab UI. The indicator transitions/animates in width and left position as a new tab is clicked. But, for small/mobile screens, a different UI is used (a select dropdown). Both the select and the tabs utilize the same data, let's call it To render the width/left position of the indicator, I have a computed method, let's call it The problem arises if the user changes the My current solution is to move the tabs offscreen via CSS (instead of doing a Sorry for the length. I do think the enhancement warrants further investigation and could be useful :) |
What if a computed property depends on a server call and I want to offer a refresh button? Could be a good use case for |
Like inline expressions, Vue.js automatically collects dependencies for computed properties and refreshes/recomputes them when their dependencies change. All you may need to do is reference those dependencies for which you need a recomputation. Check following for example: http://optimizely.github.io/vuejs.org/guide/computed.html |
I have a very similar use-case to @benjarwar. I currently do so by retrieving the element's Is there a way around this, @nwaughachukwuma? Because if not, I'm also in favor of I think I realize that I could retrieve this data in an interval and update my component's data instead of using a computed property. EDIT:
This is in my opinion relatively little boilerplate (although as I said very dirty) and let's you recompute on demand. |
@yyx990803 can we get a reconsideration of this functionality? I think there are several use cases outlined above where I get that |
I have another use case 👍 now i need to refersh markers every 5 mins. i am not able to do that without wiring it up thru vuex store which seems wrong. i should be able to ask vue to recompute markers every 5 mins... that should be the ideal solution here. |
Greetings, |
Adding a recompute method is not very data-driven. As such, I don't expect the core team to implement this ever. Especially consider that Evan has already provided a far easier to understand solution as the second comment on this issue. It will make more sense once you understand Vue's data-driven dependency tree. |
It can also be implemented in userland as a plugin, I played around with it and will publish a plugin soon just to show how easy it is to expand Vue features 😄 But, you should really revisit the way you are designing your data. I have never found myself in the need of such thing building Vue apps, and the points said by @sirlancelot are true |
Here's what I could come up with: https://codepen.io/sirlancelot/pen/JBeXeV It uses a Vue internal mechanism though... |
that's pretty neat! This is what I did: http://jsfiddle.net/5y32ano7/2/ I added a separated |
@posva thanks! I extended your idea to include computed properties which use getters and setters: http://jsfiddle.net/0wmsvp27/3/ |
BTW, I put that into small plugin: https://github.com/posva/vue-recomputed |
@posva thanks for this plugin (BTW I found a typo in README:
|
send a PR! 😄 |
I ran into a case that had me really wishing to be able to do this:
I created an arbitrary data binding ( |
@thedamon I created a |
We need $recompute()... Come on, please 👍 There is still no good solution for this almost 6 years later? |
Literally the comment right above yours... |
Was in need of this recently. For the few cases where this is really needed (i.e. where you would introduce a data property with a counter or another random value just to invalidate stuff), I made a tiny Check it out if you know what you are doing. 😉 |
Still no support for $recompute...? |
Does this.$forceUpdate cause computed to recompute? |
@thedamon No, it just re-creates the DOM of the component based on its state, but it doesn't modify the state (including computed). |
was looking for the same thing but eventually disabling cache in computed property solved it in my case. Maybe it would be helpful for someone else here too: Example:
Found it here |
Note that disabling the cache is probably a really bad idea. You will basically wind up re-computing that value every time it is accessed which, depending on your discipline, could be multiple times per render. I believe the |
Maybe this article has the solution. |
Unless your computed value accesses a reactive property (this article give a great explaination of reactive properties), then the computed method will not know to re-run. You can manually force a computed method to re-run by calling:
Note that you must manually updated after, as the forced compute doesn't seem to cause a re-render. |
FYI for anyone else, just make it a method rather than computed. Much better for your stress levels. |
@aliakhtar Computed properties with cache disabled are equivalent to methods without arguments. Indeed, this is all about cached computed properties. If cache is disabled for the computed property, then it would always be computed when accessed. |
The problem with no As many have stated before, all the current solutions feel more like anti-patterns than an actual Maybe an annotation or an attribute that explicitly means "this getter has a dependency outside of the component, and should be recomputed on this event or time interval" would satisfy both camp |
FWIW, my scenario is that I'm creating a wrapper class My client code wants to get the currentTime as if In my
It works for YouTube video but doesn't work for local video because I don't want to change this I ended up just creating a local reactive variable.
|
@ryan-olejnik post was the solution for me. So I added a wrapper helper to my main boot code: Vue.prototype.$forceCompute= function(computedName, forceUpdate /* default: true */) {
if (this._computedWatchers[computedName]) {
this._computedWatchers[computedName].run();
if (forceUpdate || typeof forceUpdate == 'undefined') this.$forceUpdate()
}
} Then you can call in your view or component this.$forceCompute('title')
this.$forceCompute('title', false) // No force update recompute as a name seems like a "patch" to a bug. So $forceCompute has more sense, following the same idea as $forceUpdate method. |
Apparently this doesn't work anymore on Vue3. Can anybody confirm? |
Here is my take on it, using composition API: https://stackoverflow.com/a/74403125/836995 |
Is there a way to force re-computation of a computed property when none of the values the property depends on have changed?
I have a computed property that depends on some of the model data as well as the current time. So, the value of the computation changes over time. I'd like to force re-computation on an interval to capture the time dependency. Right now, I'm using a value on the root VM that is being toggled on an interval is a "dummy" dependency of the computed property. This works, but it seems like there should be a better solution.
The text was updated successfully, but these errors were encountered: