2014-07-23 89 views
2

我想通過其他自定義元素(localized_element)來更新屬性標籤在自定義元素(signin_element)。但在操作過程中,物業標籤已將地圖類型更改爲字符串。DART POLYMER:我如何從另一個自定義元素更新屬性?

說明:

1 /就在應用程序啓動後,自定義元素(登入元素)更新屬性標籤與地圖(查看文件signin.dart)。

2 /自定義元素本地化元素後初始化,物業標籤與來自外部的JSON文件的新地圖更新。方法更新無法將新的Map分配給屬性標籤,因爲現在是字符串

鉻回報警告:

Attributes on signin-element were data bound prior to Polymer upgrading the element. This may result in incorrect binding types.

Attributes on localized-element were data bound prior to Polymer upgrading the element. This may result in incorrect binding types.

最後返回一個錯誤:

Class 'String' has no instance method '[]='. 

enter image description here

Signin.html:

<core-animated-pages selectedindex="0" notap id="core_animated_pages"> 
    <section id="section1" layout horizontal active center-justified center> 
     <core-card id="core_card" layout vertical> 
     <paper-input label="Your email addres" floatinglabel id="email_input"></paper-input> 
     <paper-input label="Your password" floatinglabel id="password_input"></paper-input> 
     <div id="div" layout horizontal end-justified> 
      <paper-fab icon="check" id="paper_fab"></paper-fab> 
     </div> 
     <h2>{{labels['hello']}}</h2> 
     </core-card> 
    </section> 
    </core_animated_pages> 

    <localized-element id="l10n" locale={{locale}} labels={{labels}}></localized-element> 

signin.dart

@CustomTag('signin-element') 
class Signin extends PolymerElement { 

    @published String locale = 'en'; 

    @observable Map labels = toObservable({ 
    'hello': 'Hello 1' 
    }); 

    Signin.created() : super.created(); 
} 

localized.dart

@CustomTag('localized-element') 
class Localized extends PolymerElement { 

    @published String locale = 'en'; 
    @published Map labels; 

    Localized.created() : super.created(); 

    ready() { 
    super.ready(); 
    _loadTranslations(); 
    } 

    update() { 
    if (!_l10n.containsKey(locale)) return; 

    var l10n = _l10n[locale]; 

    labels['hello'] = l10n['hello']; 
    } 

    List locales = ['en', 'fr']; 
    _loadTranslations() { 
    locales.forEach((l10n)=> _loadLocale(l10n)); 
    } 

    Map _l10n = {}; 
    _loadLocale(_l) { 
    HttpRequest.getString('i18n/translation_${_l}.json') 
     .then((res) { 
     _l10n[_l] = JSON.decode(res); 
     update(); 
     }) 
     .catchError((Error error) { 
     print(error.toString()); 
     }); 
    } 

} 

回答

3

我認爲你需要改變

@published Map labels; 

在localized.dart到

@PublishedProperty(reflect: true) Map labels; 

同樣與locale

看到 Google Groups - Dart Web Development - PSA: Polymer 0.11.0 @published properties no longer reflect to attributes by default

+0

岡特您好,感謝再次幫助我:)。我想你說的是PublishedProperty而不是PublishedAttribute的權利?如果是的話,問題總是一樣的。我無法使用新的Map更新標籤,因爲該類型是一個字符串。 – olituks

+1

你能發佈一個可以在GitHub上重現問題的工作示例嗎?感謝提示,我更新了答案。 –

+0

聲音:https://drive.google.com/file/d/0B7kQatBvjEOzMTVFN1ZZeEdab3c/edit?usp=sharing要保存完整的zip文件,您可以使用文件 - >下載 – olituks

1

君特找到了解決方案,我把代碼變化情況下您會遇到同樣的問題。

首先,更新聚合物的依賴於開發:

polymer: '>=0.12.0-dev <0.13.0' 

其次,頂替@published從本地化。鏢

@PublishedProperty(reflect: true) String locale = 'en'; 
@PublishedProperty(reflect: true) Map labels; 

,並在文件signin.dart

@PublishedProperty(reflect: true) 
相關問題