2014-07-23 52 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) 
相关问题