在Polymer.dart中@ observable和@published之间有什么区别?

Bla*_*ard 10 dart dart-polymer

在polymer.dart中,如果要将控制器中定义的变量公开给视图端,则@observable使用双胡子来定义变量以嵌入变量.但是,一些文档和教程用于@published满足相同的目的.官方文档也使用两者,所以我不认为@published是遗留和弃用的定义变量的方式.

那两者之间有什么区别吗?我应该在代码中使用哪一个?

inv*_*ant 16

@published - 是双向绑定(模型查看和查看模型)

如果您的模型属性也是标记中的属性,则@published的用例是.

示例:对于要从外部源提供数据的表元素,因此您将定义属性数据,在这种情况下,data属性应为@published.

<polymer-element name = "table-element" attributes ="structure data">
     <template>
         <table class="ui table segment" id="table_element">
             <tr>
                 <th template repeat="{{col in cols}}">
                     {{col}}
                 </th>
             </tr>
             <tr template repeat="{{row in data}}">
              etc......
     </template>
<script type="application/dart" src="table_element.dart"></script>
</polymer-element>


@CustomTag("table-element")
class Table extends PolymerElement {

 @published  List<Map<String,String>>  structure; // table struture column name and value factory
 @published List<dynamic> data; // table data
Run Code Online (Sandbox Code Playgroud)

@observable - 是单向绑定 - (要查看的模型)

如果您只想将数据从模型传递到视图,请使用@observable

示例:要使用上面的表元素,我必须提供数据,在这种情况下,数据和结构将在我的table-test dart代码中可观察到.

<polymer-element name = "table-test">
 <template>
     <search-box data ="{{data}}" ></search-box>
     <table-element structure ="{{structure}}" data = "{{data}}" ></table-element>
 </template>
<script type="application/dart" src="table_test.dart"></script>
</polymer-element>
Run Code Online (Sandbox Code Playgroud)

飞镖码

CustomTag("table-test")
class Test extends PolymerElement {

  @observable List<People> data = toObservable([new People("chandra","<a href=\"http://google.com\"  target=\"_blank\"> kode</a>"), new People("ravi","kiran"),new People("justin","mat")]);
  @observable List<Map<String,String>> structure = [{ "columnName" : "First Name", "fieldName" : "fname"},
                                                     {"columnName" : "Last Name", "fieldName" : "lname","cellFactory" :"html"}];
  Test.created():super.created();
Run Code Online (Sandbox Code Playgroud)

示例来自My Repo