【问题标题】:Polymer Data Binding within custom element自定义元素中的聚合物数据绑定
【发布时间】:2014-09-17 10:14:17
【问题描述】:

我已经按照 http://www.polymer-project.org/docs/polymer/binding-types.html#iterative-templates 中的示例进行操作,但我没有工作代码。

我正在尝试为我正在制作的怪物创建工具制作一个形状聚合物元素,因为我的一个朋友对怪物战斗游戏有很好的想法,我想帮助他跟踪他的想法。

所以我为怪物属性字段制作了一个自定义元素。由于我们不能 100% 确定所有统计数据最终会是什么,因此我希望使其尽可能模块化。

<link rel="import" href="../../../bower_components/paper-input/paper-input.html">

<!-- Calling noscript until I can better understand -->
<polymer-element name="monster-stat-field"
attributes = "stat value"
noscript
>

<template>
    <div id="name">
        {{stat}}
    </div>
    <div id="value">
        <paper-input
            type="number"
            error="Required, needs to be a number!"
            required
            label="{{stat}}"
            value="{{value}}"
            layout vertical>
        </paper-input>
    </div>
</template>

</polymer-element>

当然,这应该继承纸质输入。

我所做的是将它嵌套在另一个自定义聚合物元素中:

<link rel="import" href="../monster-stat-field/monster-stat-field.html">
<link rel="import" href="../../../bower_components/paper-input/paper-input.html">
<link rel="import" href="../../../bower_components/paper-button/paper-button.html">

<!-- Calling noscript until I can better understand -->
<polymer-element name="monster-form"
attributes = "name desc stats"
noscript
>
<template>
    <p>
        <paper-input
            label="Monster Name"
            value="{{name}}"
            required
            floatingLabel
            error="Required Field!">
        </paper-input>
    </p>
    <p>
        <paper-input
            label="Description"
            value="{{desc}}"
            floatingLabel>
        </paper-input>
    </p>
    <template repeat="{{s in stats}}">
            <p>
                <monster-stat-field stat="{{s.name}}" value="{{s.value}}">
                </monster-stat-field>
            </p>
    </template>

    <paper-button label="Save" raisedButton></paper-button>
</template>

</polymer-element>

然后在我的主页上:

<link rel="import" href="static/customComponents/monster/monster-form/monster-form.html">
...some other stuff...
    <monster-form
        name="Amalgam"
        desc="The Liquid Metal"
        stats="[
            {name: 'hp', value: 60},
            {name: 'cost', value: 60},
            {name: 'ene', value: 90},
            {name: 'str', value: 90},
            {name: 'wis', value: 75},
            {name: 'def', value: 95},
            {name: 'man', value: 90},
            {name: 'agi', value: 100}
            ]"
    ></monster-form> <br/>
    <p>Here is calling the monster stat field manually</p> <br/>
    <monster-stat-field stat="HP" value="60">
    </monster-stat-field>

所以!我显示的是怪物的名称和描述,但统计数据根本不存在。手动调用它确实有效,但我觉得这很奇怪,因为我没有明确导入它。

这是因为我在调用 noscript 吗?或者是别的什么?任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: polymer


    【解决方案1】:

    您需要更改两件事才能使其正常工作:

    stats 属性的 JSON 字符串不正确。您需要在哈希键周围加上双引号:

    stats='[
                {"name": "hp", "value": 60},
                {"name": "cost", "value": 60},
                {"name": "ene", "value": 90},
                {"name": "str", "value": 90},
                {"name": "wis", "value": 75},
                {"name": "def", "value": 95},
                {"name": "man", "value": 90},
                {"name": "agi", "value": 100}
    ]'
    

    您需要提示stats 属性的类型,以便Polymer 知道它需要将其转换为数组。从monster-form 元素中移除noscript 属性并添加created 处理函数:

    <script>
        Polymer('monster-form', {
            created: function() {
                this.stats = [];
            }
        });
    </script>
    

    现在 Polymer 会将stats 属性字符串转换为(对象)数组。

    【讨论】:

    • 几个小注意事项:(1)最新版本的 Polymer 会在尝试 JSON 转换之前为您将单引号转换为双引号(但 JSON 仍然需要您引用所有键)。 (2) 你可以通过简单地将stats 放在你的原型上(如stats: [])来输入提示,只要你承诺永远不会push (不先创建一个新数组)。
    • 好点,在这种情况下我不会,这些应该只是存储值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多