聚合物绑定属性未更新

时间:2016-11-21 23:24:30

标签: javascript polymer

我为Polymer写了一个简单的i18n元素。我们的想法是下载翻译,然后将其缓存在本地存储中。我对以下代码有疑问,几乎逐字逐句地从https://github.com/PolymerElements/app-storage#polymerappstoragebehavior

获取
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<link rel="import" href="../app-storage/app-localstorage/app-localstorage-document.html">

<dom-module id="x-trans">
    <template>
        <iron-ajax id="ajax"
            handle-as="json"
            last-response="{{translation}}"
        ></iron-ajax>
        <app-localstorage-document session-only log id="localstorage"
            key="x-trans-translation"
            data="{{translation}}"
        >
    </template>
    <script>
        Polymer({
            is: 'x-trans',
            properties: {
                translation: {
                    type: Object,
                    value: {},
                    notify: true
                }
            });
    </script>
</dom-module>

在我看来,这应该:

  • 使用默认值translation声明属性{}
  • 获取文件(在运行时配置URL)并将响应保存到translation
  • translation的内容存储在本地存储中。

然而,在以下测试中:

test('retrieving translated string', function() {
                        var element = fixture('ajax');
                        request = element.$.ajax.generateRequest();
                        server.respond();
                        expect(request.response).to.be.ok;
                        expect(request.response).to.be.an('object');
                        expect(request.response['Hello world!']).to.be.equal('World, hello!');
                    });

app-localstorage日志输出:

Got stored value! undefined Object {  }

在我看来,translation以某种方式保留了它的默认值,尽管受到约束,应根据文档对其进行更新。谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

您显示的代码看起来是正确的(除了省略的iron-ajax网址和server的设置)。 (demo

一对夫妇注意到:

  • translation的默认值应该是返回空对象的函数。将其直接设置为对象会导致对象在元素的所有实例中共享,这可能不是您想要的。

  • handle-as="json"是多余的,因为这是默认设置

我怀疑app-localstorage的日志表明您获得了一个空对象,因为您的模拟server可能设置错误。

相关问题