Search code examples
polymerpolymer-2.x

Why doesn't my this._setPropertyName function work?


I have the following polymer element :

<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../shared-styles.html">
<link rel="import" href="../../bower_components/dfw-styles/dfw-styles.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/paper-menu-button/paper-menu-button.html">
<link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html">

<dom-module id="baseline-policy-create">
  <template>
    <style include="dfw-styles">
      :host {
        display: block;
      }
      .top-button{
        float : right;
      }
    </style>

    <div class="outer-buttons">
      <paper-menu-button horizontal-align="right" no-overlap="true" no-animations class="top-button">
        <paper-button slot="dropdown-trigger" class="dropdown-trigger create-button btn-primary">
          <iron-icon icon="menu"></iron-icon>
          <span>Create Baseline Policy</span>
        </paper-button>
        <paper-listbox slot="dropdown-content" class="dropdown-content">
          <template is="dom-repeat" items="{{_domains}}">
            <paper-item on-tap="getDomainSchema">[[item.label]]</paper-item>
          </template>
        </paper-listbox>
      </paper-menu-button>
    </div>
  </template>
  <script>
    class BaselinePolicyCreate extends Polymer.Element {
      static get is() {
        return 'baseline-policy-create';
      }

      static get properties() {
        return {
          _domains: {
            type: Array,
            value: [{'name':'Package', 'label':'Package'},
                    {'name':'Subscription', 'label':'Subscription'}] //TODO: is it possible to get these values from an API source
          },
          requestedDomain: {
            type: String,
            value : "",
            notify : true,
            readOnly : true
          }
        }
      }

      getDomainSchema(evt) {
        console.info("Get the schema for the following domain:", evt.target.innerText);
        console.log(this.requestedDomain);
        this._setRequestedDomain(evt.target.innerText);
        //this.requestedDomain = evt.target.innerText;
        console.log(this.requestedDomain);
      }
    }
    customElements.define(BaselinePolicyCreate.is, BaselinePolicyCreate);
  </script>
</dom-module>

I've been following this tutorial on data binding : https://www.tutorialspoint.com/polymer/polymer_data_system.htm

In the example, the code for prop-element has a property, myProp, with the attribute readOnly set to true. In the Onclick function, its still able to change the value of the property using this._setMyProp() which isn't defined anywhere explicitly.

I want to do the same in my code. That is, I want to set requestedDomain using this method. I can set it using this line :

this.requestedDomain = evt.target.innerText;

But to do so, I can't set the readOnly flag to true. If I use this._setRequestedDomain, I get an error saying it is not a function. Am I missing some import at the top to allow this to work, or maybe its been removed in Polymer 2?


Solution

  • I have been testing your code and it's ok.

    You only will get the error "this.setRequestedDomain is not a function" if your property is set as readOnly = false.
    
    Please, try again and tell me if its ok.
    

    pen code example (without styling)