【问题标题】:Cross-DOM observers in PolymerPolymer 中的跨 DOM 观察者
【发布时间】:2015-11-30 21:06:51
【问题描述】:

我在 Polymer 中为 i18n 开发了一个组件。 它基于<iron-localstorage> 存储和更改语言环境。

<iron-localstorage name="marvin-locale-ls"
                           value="{{locale}}"
                           on-iron-localstorage-load-empty="initializeDefaultLocale"
        ></iron-localstorage>
<script>

    MarvinLocaleLS = Polymer({
        is: 'marvin-locale-ls',

        properties: {
            locale: {type: String},
            ...

我还有一个翻译器组件,可以根据这个语言环境进行翻译。 我想做这样的事情:

<script>
        Polymer({
            is: 'marvin-translate',
            ls: new MarvinLocaleLS(),
            properties: {
                key: {
                    type: String,
                    notify: true
                },
                locale: {
                    type: Polymer.dom().querySelector('marvin-locale-ls').properties.locale,
                    observer: '_localeObserver'
                }
            },

            ready: function(){
                this.key = this.textContent;
                var t = this.ls.getTranslation(this.key); // get translation from Local Storage
                this.textContent = (t) ? t : this.key; // show translation or key if there is no translation
            },
            _localeObserver: function(){
                console.log('locale changed')
            }

        });


    </script>

换句话说,我想在“marvin-translate”中为“marvin-locale-ls”中的属性创建观察者。有可能吗?

【问题讨论】:

标签: javascript polymer web-component


【解决方案1】:

看看这个组件,它可以让你在 Key/Value 的基础上共享变量并在任何你想要的地方访问它们。它还支持数据绑定(iron-meta 尚不支持):https://github.com/akc42/akc-meta

【讨论】:

    猜你喜欢
    • 2016-05-23
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2018-09-15
    • 1970-01-01
    • 2015-04-08
    相关资源
    最近更新 更多