我正在为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
使用默认值声明属性{}
,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
,尽管受到约束,它仍会以某种方式保留其默认值,这应该根据文档进行更新。谁能告诉我我在做什么错?
您显示的代码看起来正确(除了省略的iron-ajax
URL和的设置server
)。(演示)
几个注意事项:
的默认值translation
应该是一个返回空对象的函数。将其直接设置为对象将导致该对象在元素的所有实例之间共享,这可能不是您想要的。
handle-as="json"
是多余的,因为这是默认设置
我怀疑app-localstorage
的日志表明您收到的对象为空,因为您的模拟程序server
可能设置不正确。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句