从打包库导入的模型类?

时间:2013-12-23 19:41:12

标签: dart dart-polymer dart-pub

在导入的打包库中定义模型类时,在聚合物元素中使用模型的正确方法是什么?

我相信我做得对(基于更多文章,例如How to subscribe to change of an observable field)。

它适用于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构建。当我尝试写“hello”这个词时,我得到:

DARTIUM - 好的

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"

2 个答案:

答案 0 :(得分:1)

当依赖包中存在聚合物变换器时,pub构建生成的js非常不同,尤其是检测和传播可观察变化所涉及的js代码。

尝试将聚合物变换器添加到导入包pubspec:

name: testlib dependencies: browser: any observe: any polymer: any transformers: - polymer: entry_points:

(注意,不需要实际的entry_points值,只需要存在聚合物变压器入口。)

答案 1 :(得分:0)

你说模型是共享的很重要。 模型是否真的绑定在另一个地方? 在您的示例中,MyModel仅用于MyForm。

当您致电pub build --mode debug时,生成的JavaScript非常易读。 也许您可以尝试调试JavaScript代码获得一些其他信息。

相关问题