Search code examples
javascriptpolymerpolymer-1.0

Access element scope from dom-repeat items compute method


How can one access the element-scope (this) in a method which is called by a dom-repeater to return items?

Take a look at this example:

<dom-module id="demo-element">
    <template>
        <template is="dom-repeat" items="[[getItems()]]">
            <div>[[item]]</div>
        </template>
    </template>
    <script th:inline="javascript">
        Polymer({
            is : "demo-element",
            properties: {
                begin: { type: Number, value: 9 }
            },
            getItems: function() {
                console.log(this.begin); // <- logs "undefined"
                return [0,1,2,3,4];
            }
        });
    </script>
</dom-module>

this.begin can not be accessed from inside getItems().


Solution

  • The solution is to list the properties which you need to access like this:

    <template is="dom-repeat" items="[[getItems(begin)]]">