在导入的打包库中定义模型类时,在聚合物元素中使用模型的正确方法是什么?
我相信我做得对(基于更多文章,例如如何订阅可观察字段的更改)。
它可以在 DARTIUM 中运行,但是当它构建到 JS 时就不行了。我无法弄清楚那里发生了什么,请参阅日志。字段中的侦听器和值表现得很奇怪。
关键因素:我的模型类由于某些原因被打包在共享库中,并且必须使用我的输入元素
我错过了什么?或者 dart2js 中是否存在错误?
文件和日志
testlib.dart(只是打包库中的一个模型,不是聚合物应用程序的一部分):
library testlib;
import 'package:observe/observe.dart';
class MyModel extends Object with Observable{
@observable String foo;
String toString(){
return "MyModel[foo:$foo]";
}
}
在聚合物应用程序中,我们有:
all_elements.dart(模型是使用包导入的:...)
library test;
import 'package:polymer/polymer.dart';
import 'package:testlib/testlib.dart'; //!!!! note this is important, I need shared model
@CustomTag('my-input')
class MyInput extends PolymerElement {
@published String value;
valueChanged(oldValue){
print('text changed from "$oldValue" to "$value"');
}
MyInput.created() : super.created();
}
@CustomTag('my-form')
class MyForm extends PolymerElement {
@observable MyModel model = new MyModel();
modelChanged(oldValue){
print('text changed from "$oldValue" to "$model"');
}
MyForm.created() : super.created();
void ready(){
model.changes.listen((changes){
print('model properties changed $changes');
});
}
}
和all_elements.html
<polymer-element name="my-input">
<template>
<p>My Input</p>
<input type="text" value="{{value}}"/>
</template>
</polymer-element>
<polymer-element name="my-form">
<template>
<my-input value="{{model.foo}}"></my-input>
</template>
</polymer-element>
<script type="application/dart" src="all_elements.dart"></script>
和应用程序入口点:testapp.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test app</title>
<link rel="import" href="all_elements.html">
<script type="application/dart">export 'package:polymer/init.dart';</script>
<script src="packages/browser/dart.js"></script>
</head>
<body>
<my-form></my-form>
</body>
</html>
日志:
在构建到 JS 之前,一切正常。当试图写单词“你好”来输入时,我得到:
达蒂姆-好的
text changed from "null" to "h"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: null to: h>]
text changed from "h" to "he"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: h to: he>]
text changed from "he" to "hel"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: he to: hel>]
text changed from "hel" to "hell"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: hel to: hell>]
text changed from "hell" to "hello"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: hell to: hello>]
JavaScript - 问题(输入“hello” - 获取“hel”,输入“mystery” - 获取“mytr”)
text changed from "null" to "h"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: null to: h>]
text changed from "h" to "he"
text changed from "he" to "h"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: h to: he>]
text changed from "h" to "hl"
text changed from "hl" to "he"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: he to: hl>]
text changed from "he" to "hel"
text changed from "hel" to "hl"
model properties changed [#<PropertyChangeRecord Symbol("foo") from: hl to: hel>]
text changed from "hl" to "hlo"
text changed from "hlo" to "hel"