【发布时间】:2016-11-21 23:24:30
【问题描述】:
我正在为 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 以某种方式保留了它的默认值,尽管被绑定,它应该根据文档对其进行更新。谁能告诉我我做错了什么?
【问题讨论】:
标签: javascript polymer