Vue JS Watching deep nested object

前端 未结 4 1947
深忆病人
深忆病人 2021-02-04 23:47

Disclaimer: This is my first attempt at building an MVVM app I have also not worked with vue.js before, so it could well be that my issue is a result of a more

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-05 00:29

    Since nobody replied and I have solved/ worked around the issue by now, I thought it migth be useful to post my solution. Please note that I am not sure my solution is how these types of things should be tackled, it works though.

    Instead of using this event listener v-on="change: checkboxChange(this)" I am now using a custom directive which listens to both the selected and disabled model attribute, like this: v-on-filter-change="selected, disabled".

    The directive looks like this:

    directives: {
        'on-filter-change': function(newVal, oldVal) {
            // When the input elements are first rendered, the on-filter-change directive is called as well, 
            // but I only want stuff to happen when a user does someting, so I return when there is no valid old value
            if (typeof oldVal === 'undefined') {
                return false;
            }
            // Do stuff here
            // this.vm is a handy attribute that contains some vue instance information as well as the current object
            // this.expression is another useful attribute with which you can assess which event has taken place
        }
    },
    

    The if clause seems a bit hacky, but I couldn't find another way. At least it all works.

    Perhaps this will be useful to someone in the future.

提交回复
热议问题