javascript - Pass function call as attribute in Polymer? -
my polymer element has array attribute anywhere 1-10,000 items, need generate when element instantiated. however, really pass in array using function call or reference:
<script> function genarray(size){ var = []; for(var = 0, count = size - 1; < count; i++){ a.push("domain" + count + ".tld"); } return a; } </script> <my-element domains="genarray()"></my-element>
or reference global object.
<script> function genarray(size){ var = []; for(var = 0, count = size - 1; < count; i++){ a.push("domain" + count + ".tld"); } return a; } window.myarray = genarray(1000); </script> <my-element domains="window.myarray"></my-element>
i've tried variations on quote style (""
vs ''
) brackets ("{{genarray}}"
) , '{"array":window.myarray}'
.
i understand pass in parameter or reference global object , process them in lifecycle callback. however, cleaner , simplify testing/deployment if pass in call external function. screwed until template strings comes along in es7?
bindings work within polymer environment, there solution! polymer provides helper component called dom-bind
can use anywhere in page, outside of polymer element:
<script> function genarray() { ... } window.myarray = genarray(1000); </script> <template is="dom-bind"> <my-element domains="[[myarray]]"></my-element> </template>
more documentation component can found @ https://www.polymer-project.org/1.0/docs/devguide/templates.html#dom-bind
Comments
Post a Comment