在poly.dart中,如果希望将控制器中定义的变量公开给视图端,可以用@observable
定义该变量,并使用双胡子嵌入该变量.但是,一些文档和教程使用@published
来满足相同的目的.官方文档也同时使用这两种方法,所以我不认为@published
是定义变量的遗留的和不推荐使用的方式.
那么这两者有什么不同呢?我应该在我的代码中使用哪一个呢?
在poly.dart中,如果希望将控制器中定义的变量公开给视图端,可以用@observable
定义该变量,并使用双胡子嵌入该变量.但是,一些文档和教程使用@published
来满足相同的目的.官方文档也同时使用这两种方法,所以我不认为@published
是定义变量的遗留的和不推荐使用的方式.
那么这两者有什么不同呢?我应该在我的代码中使用哪一个呢?
@published - is two way binding ( model to view and view to model)个
如果模型属性也是标记中的属性,@published的用例是.
示例:对于要从外部源提供数据的表元素,您将定义属性数据,在本例中,data property应为@Publisher.
<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
@observable - is one way binding - ( model to view)个
如果您只想将数据从模型传递到视图,请使用@Observable
示例:要使用上面的表格元素,我必须提供数据,在这种情况下,数据和 struct 将在我的表格测试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>
省道代码
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();
例子取自My Repo个