Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to trigger a function upon a Vue data element change?

My API provides me a hash which I receive as part of an AJAX call. The content of the AJAX response (which includes the hash) is updating data components in my Vue instance (so that the DOM is modified, per usual Vue usage).

I was wondering if it is possible to trigger (run) a function upon the change of a specific data element. Reactivity in Depth does not mention this and for me (please correct me if this is wrong) computedand methods are a way to indirectly provide new computed elements for the DOM (in other words, they do not start because a specific element was modified but are rather synchronisation methods between data and other variables provided to the DOM).

I was hoping for something along the lines of (this is non-working, incorrect pseudo-code, I just add it to put in context of a Vue instance):

var vm = new Vue({
    el: '#root',
    data: {
      hash: null
    },
    functions_to_trigger_upon_an_element_change: {
      hash: function () {
        location.reload()
      }
    }
  })

The idea above would be to have location.reload() run when the value of hash changes.

Is there such a mechanism in Vue?

If there is not, I will keep the state independently of Vue and act accordingly upon its change but it would be nice to reuse the Vue watch properties to do that.

like image 591
WoJ Avatar asked Jan 06 '17 08:01

WoJ


People also ask

Which of the following is used to watch data changes on a Vue instance?

A Watcher in Vue. js is a special feature that allows one to watch a component and perform specified actions when the value of the component changes. It is a more generic way to observe and react to data changes in the Vue instance. Watchers are the most useful when used to perform asynchronous operations.

What is reactive dependency in Vue?

Reactive dependencies are those that are observable, like data properties or Vuex state. Non-reactive dependencies would be something like a simple variable or something from local-storage.

Are Vue methods reactive?

Vue comes with the reactive method, which is used to create a reactive state in JavaScript or Vue. Basically, the reactive method is just a function that creates a proxy and wraps it around provided data objects, ultimately turning it into a proxied object.

Why must Vue component data be a function?

It must be a function because otherwhise the data will be shared among all instances of the component, as objects are call by reference rather than call by value. This does not only happen when you reference a global object but also when data is an object itself.


2 Answers

You can use a watch, like the following example:

var vm = new Vue({
  el: '#demo',
  data: {
    firstName: 'Foo',
    lastName: 'Bar',
    fullName: 'Foo Bar'
  },
  watch: {
    firstName: function (val) {
      this.fullName = val + ' ' + this.lastName
    },
    lastName: function (val) {
      this.fullName = this.firstName + ' ' + val
    }
  }
})

About your case, here is the example about how it could be implemented.

var vm = new Vue({
  el: '#demo',
  data: {
    hash: 'Foo' 
  },
  watch: {
    hash: function (val) {
      // when the hash prop changes, this function will be fired.
      location.reload()
    } 
  }
})

I hope I understood your question right.

like image 166
Pedro Augusto Freitas de Souza Avatar answered Oct 14 '22 12:10

Pedro Augusto Freitas de Souza


I think this should work

var vm = new Vue({
    el: '#root',
    data: {
      hash: null
    },
    watch: {
        hash: function (val) {
          functions_to_trigger_upon_an_element_change()
        }
    },
    methods: {
        functions_to_trigger_upon_an_element_change() {
            // You code
        }
    }
 })
like image 36
DokiCRO Avatar answered Oct 14 '22 11:10

DokiCRO