代码之家  ›  专栏  ›  技术社区  ›  Ofisora

如何访问聚合物元素模板内js文件中定义的函数?

  •  1
  • Ofisora  · 技术社区  · 8 年前

    global.function.js 文件为

    function getData(flag) {
    if (flag === 1) {
      return "one";
     } 
    else {
      return "not one";
     }
    }
    

    然后使用 元素:

    <script src="global.function.js"></script>

    自定义元素.html ,我可以在脚本部分访问它,但不能在模板部分访问。 有什么方法可以访问HTML元素中的函数吗?

    <!-- custom-element.html -->
    <link rel="import"  href="https://polygit.org/components/polymer/polymer-element.html">
    <link rel="import" href="custom-js-import.html">
    
    <dom-module id="custom-element">
    
      <template>
        <div>
          Hello
        </div>
        <div id="data"></div>
        <div>{{getData(1)}}</div><!-- Unable to access this from here -->
        <div>{{getLocalData()}}</div>
      </template>
    
    <script>
      // Define the class for a new element called custom-element
      class CustomElement extends Polymer.Element {
        static get is() { return "custom-element"; }
        constructor() {
            super();
          }
    
          ready(){
            super.ready();
            this.$.data.textContent = "I'm a custom-element.";
            console.log(getData(1));//can be easily accessed from here
          }
    
          getLocalData(){
            return "local";
          }
    
      }
      // Register the new element with the browser
      customElements.define(CustomElement.is, CustomElement);
    </script>
    </dom-module>
    

    Sample Code

    2 回复  |  直到 8 年前
        1
  •  2
  •   Rico Kahler    8 年前

    有什么方法可以访问HTML元素中的函数吗?

    Polymer的数据绑定系统设计用于将值绑定到模板。这些值通常只是文字(例如字符串和数字)或普通ole javascript对象,例如。 {a: 'someval', b: 5} 聚合物的数据绑定系统是 设计用于将函数绑定到模板,您不能仅在模板内使用javascript。 (If you're really into that idea, check out React as a replacement to polymer) .

    computed property 。创建一个对其他变量的更改做出反应的计算属性,而不是在模板内调用函数。当特性的状态更改时,计算的特性也将更改。这种状态可以被视为函数的参数。

    <link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
    <link rel="import" href="custom-js-import.html">
    
    <dom-module id="custom-element">
    
      <template>
        <div>
          Hello
        </div>
        <label>
          <input type="number" value="{{flag::input}}">
        </label>
        <h1>from flag: [[flag]]</h1>
        <div id="data"></div>
        <div>{{boundComputedData}}</div><!-- Unable to access this from here -->
        <div>{{getLocalData()}}</div>
      </template>
    
      <script>
        // Define the class for a new element called custom-element
        class CustomElement extends Polymer.Element {
          static get is() {
            return "custom-element";
          }
          constructor() {
            super();
          }
    
          getData(flag) {
            const flagAsNumber = parseInt(flag);
            if (flagAsNumber === 1) {
              return "one";
            } else {
              return "not one";
            }
          }
    
          ready() {
            super.ready();
            this.$.data.textContent = "I'm a custom-element.";
            console.log(this.getData(1)); //can be easily accessed from here
          }
    
          getLocalData() {
            return "local";
          }
    
    
    
          static get properties() {
            return {
              flag: {
                type: Number,
                value: 0
              },
              boundComputedData: {
                type: String,
                computed: 'getData(flag)'
              }
            };
          }
    
        }
        // Register the new element with the browser
        customElements.define(CustomElement.is, CustomElement);
      </script>
    </dom-module>
    
    <custom-element></custom-element>

    所以我在这里做的是:

    boundComputedData 并设置 computed 属性到 'getData(flag)' getData .

    现在只要州政府 flag 更改时,计算属性将更新。

        2
  •  2
  •   Ofisora    8 年前

    Rico Kahler 建议我使用 mixin .使用 解决了我的问题。您可以查看完整的工作示例 here .

    <!--custom-mixin.html-->
    <script>
      const CustomMixin = superclass => class extends superclass {
    
    static get properties() {
      return {};
    }
    
    connectedCallback() {
      super.connectedCallback();
    }
    
    getData(flag) {
       if (flag === 1) {
        return "one(From Mixin)";
       } else {
        return "not one(From Mixin)";
       }
      }
     };
    </script>
    

    记住导入mixin文件并将该mixin添加到元素中。

    <!-- custom-element.html -->
    <link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
    <link rel="import" href="custom-mixin.html">
    
    <dom-module id="custom-element">
    
      <template>
        <div>
          Hello
        </div>
        <div id="data"></div>
        <div>{{getData(1)}}</div>
        <!-- Unable to access this from here -->
        <div>{{getLocalData()}}</div>
      </template>
    
      <script>
        // Define the class for a new element called custom-element
        class CustomElement extends CustomMixin(Polymer.Element) {
            static get is() {
              return "custom-element";
            }
            constructor() {
              super();
            }
    
            ready() {
              super.ready();
              this.$.data.textContent = "I'm a custom-element.";
              console.log(getData(1)); //can be easily accessed from here
            }
    
            getLocalData() {
              return "local";
            }
    
          }
          // Register the new element with the browser
        customElements.define(CustomElement.is, CustomElement);
      </script>
    </dom-module>