Is there a way to monitor changes to an object?

前端 未结 3 596
南笙
南笙 2021-01-18 06:45

Instead triggering an event based on user action can you just listen for changes to an object and react then?

3条回答
  •  旧巷少年郎
    2021-01-18 07:24

    Well, depending on the domain you're working with, you can use backbone.js - it has a "model" object that you can extend and connect up to callbacks that get triggered by events automatically when the model is changed - that is, you set a field on the model, and it will automatically fire an event that you can listen to. You have to define your models to extend its Model, and call a setter function instead of setting properties directly, but it's very useful, I recommend it.

    Just for clarity's sake, the event is fired from the model on change, so it's fired even if you set the value of the attribute programmatically or in the console.

提交回复
热议问题