Observe changes for an object in Polymer JS

前端 未结 3 1995
悲&欢浪女
悲&欢浪女 2021-01-05 15:52

I have an element with a model object that I want to observe like so:

         


        
3条回答
  •  有刺的猬
    2021-01-05 16:22

    To observe paths in an object, you need to use an observe block:

    Polymer('x-element', {
      observe: {
        'model.title': 'modelUpdated',
        'model.text': 'modelUpdated',
        'model.slug': 'modelUpdated'
      },
      ready: function() {
        this.model = {
          title: this.noteTitle,
          text: this.noteText,
          slug: this.noteSlug
        };
      },
      modelUpdated: function(oldValue, newValue) {
        var value = Path.get('model.title').getValueFrom(this);
        // newValue == value == this.model.title
      }
    });
    

    http://www.polymer-project.org/docs/polymer/polymer.html#observeblock

提交回复
热议问题