2016-06-10 26 views
1
  1. 我有一個跨元素共享的行爲。
  2. 在一個元素中,我有一個on-change =「_ toggle」開關。
  3. 在第二個元素中,我從沒有開關按鈕的第一個元素獲得相同的代碼。當開關激活時,它會反射到包含開關的元件。

問:我怎樣才能在所有元素上反映on-change =「_ toggle」功能?聚合物反映功能跨越元素

我的代碼:

1.

<script> 
    LanguageBehavior = { 

    behaviors: [ 
     Polymer.AppLocalizeBehavior 
    ], 

    properties: { 
     language: { 
     value: 'sl', 
     reflectToAttribute: true 
     }, 
    }, 

    attached: function() { 
     this.loadResources(this.resolveUrl('/data/locales.json')); 
    }, 

    _toggle: function() { 
     this.language = this.$.switch.checked ? 'en' : 'sl'; 
     // This function is the problem? 
    } 

    }; 
</script> 
<link rel="import" href="../../bower_components/polymer/polymer.html"> 
<link rel="import" href="../../bower_components/paper-toggle-button/paper-toggle-button.html"> 
<link rel="import" href="../../bower_components/app-localize-behavior/app-localize-behavior.html"> 
<link rel="import" href="behavior.html"> 

<dom-module id="bazdara-element-1"> 
    <template> 
    <style> 
     :host { 
     display: block; 
    } 
    </style> 
     {{localize('menu_2')}} // THIS CHANGES WITH THE SWITCH 
     <span title="english"> SI</span> 
     <paper-toggle-button on-change="_toggle" id="switch"></paper-toggle-button> 
     <span title="french">EN</span> 
    </template> 
    <script> 
    Polymer({ 
     is: 'bazdara-element-1', 
     behaviors: [Polymer.AppLocalizeBehavior, LanguageBehavior], 
    }); 
    </script> 
</dom-module> 
<link rel="import" href="../../bower_components/polymer/polymer.html"> 
<link rel="import" href="../../bower_components/app-localize-behavior/app-localize-behavior.html"> 
<link rel="import" href="behavior.html"> 

<dom-module id="bazdara-element-2"> 
    <template> 
    <style> 
     :host { 
     display: block; 
    } 
    </style> 
     {{localize('menu_2')}} // THIS Doesnt change 
    </template> 
    <script> 
    Polymer({ 
     is: 'bazdara-element-2', 
     behaviors: [Polymer.AppLocalizeBehavior, LanguageBehavior], 
    }); 
    </script> 
</dom-module> 

回答

1

由於每個單元都會有自己的行爲的複製你的做法是不去上班。現在唯一的選擇,我可以拿出是:

  1. 保持你的元素在一個共同的元素,並在那裏改變屬性。
  2. 如果您打算將所有元素放在一個HTML文件中,則可以在該HTML文件中更改此功能並將該屬性綁定到所有元素。
+0

在進一步搜索我發現'紙烤麪包'不允許一次打開多個烤麪包。它已經在聚合物功能之外宣佈了一個變量'currentToast',並且在整個敬酒過程中保持不變。也許你可以看看它的代碼,它可能會有所幫助。 – a1626

+0

我有很多超過2個元素,我不能只使用一個元素。我想翻譯整個應用程序...順便說一下,它不是紙面吐司,我用它的紙張切換按鈕 –

+0

我的建議是看它的代碼,它可能會給你一些關於如何實現它的想法。無論如何,如果它是本地化你試圖達到那麼你爲什麼不嘗試'i18n-msg'。 [這裏是](https://ebidel.github.io/i18n-msg/components/i18n-msg/)相同的鏈接。 – a1626