Polymer 中的交叉 DOM 观察者

Cross-DOM observers in Polymer

我在 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' 中的 属性 创建观察者。可能吗?

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