4
在以下示例中,單擊Update Class按鈕時,{{testClass.someProperty}}用新類someProperty更新,但更新testClass.someProperty不更新{{testClass.someProperty}}。使所有類屬性在飛鏢PolymerElement模板中可觀察
我認爲使TestClass @observable會使其屬性可觀察,並且在更新屬性時,模板中的屬性會改變。
什麼是將TestElement.testClass.someProperty綁定到{{testClass.someProperty}},並將任何類的屬性作爲擴展名綁定到Polymer Element模板的正確方法?
test_class.dart
library test;
import "package:polymer/polymer.dart";
@observable
class TestClass extends ObservableBase {
String someProperty;
TestClass(this.someProperty);
}
test_element.dart:
library test_element;
import "package:polymer/polymer.dart";
import "test_class.dart";
@CustomTag("test-element")
class TestElement extends PolymerElement with ObservableMixin {
@observable
TestClass testClass = new TestClass("original");
void updateClass() {
testClass = new TestClass("xyz");
}
void updateProperty() {
testClass.someProperty = "foobar";
}
}
test_element.html
<polymer-element name="test-element">
<template>
{{testClass.someProperty}}
<button on-click="updateProperty">Update Property</button>
<button on-click="updateClass">Update Class</button>
</template>
<script type="application/dart" src="test_element.dart"></script>
</polymer-element>
test.dart
library test;
int main() {
}
的test.html:
<!DOCTYPE html>
<html>
<head>
<link rel="import" href="test_element.html" />
<script src="packages/polymer/boot.js"></script>
</head>
<body>
<test-element></test-element>
<script type="application/dart" src="test.dart"></script>
</body>
</html>
我遇到了同樣的問題。它似乎在不擴展ObservableBase的情況下工作(只需在屬性上添加@observable註釋)。 你填補了一個錯誤嗎? –
我實際上最近一直無法得到這個工作,但我沒有花太多時間在它上面。我沒有提交錯誤,因爲我沒有完全清楚它應該如何工作,而且我不明白'@ observable'和'@ published'之間的區別。我會盡量在今天晚些時候花點時間在它上面提交一些東西給混帳郵件列表,以確保我理解它。 – Nathanial
是的,我也找到了。除了第一次嘗試(例如構造函數)之外,它不適用於getter-s。我不認爲觀察員在這一點上是可靠的。 – will