Observe changes for an object in Polymer JS

前端 未结 3 1998
悲&欢浪女
悲&欢浪女 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:03

    Or you can add an extra attribute to your model called for example 'refresh' (boolean) and each time you modify some of the internal values also modify it simply by setting refresh = !refresh, then you can observe just one attribute instead of many. This is a good case when your model include multiple nested attributes.

    Polymer('x-element', {
      observe: {
        'model.refresh': 'modelUpdated'
      },
      ready: function() {
        this.model = {
          title: this.noteTitle,
          text: this.noteText,
          slug: this.noteSlug,
          refresh: false
        };
      },
      modelUpdated: function(oldValue, newValue) {
        var value = Path.get('model.title').getValueFrom(this);
      },
      buttonClicked: function(e) {
        this.model.title = 'Title';
        this.model.text = 'Text';
        this.model.slug = 'Slug';
        this.model.refresh = !this.model.refresh;
      }
    });
    

提交回复
热议问题