我有一个 Dart WebComponent
,它定期从 Web 服务获取信息。我想将 Web 服务注入组件并让组件决定何时调用 Web 服务(这将允许我使用模拟 Web 服务对所有 UI 代码进行原型设计,在编写服务之前没有 HTTP 调用)。
我遇到的问题是,在呈现页面之前,我发送到的 Web 服务对象WebComponent
似乎为空。我不确定何时将服务引用传递给页面,但它似乎发生在页面构建之后,因为构造函数中的值为 null WebComponent
,但我可以看到该值是在页面本身呈现时实例化的. 如何通知我服务对象现在可用于页面,以便我可以调用 Web 服务?
后续问题:将服务引用传递给WebComponent
我下面的类似错误做法吗?如果是这样,我应该做些什么来分离模拟实现,以便以后可以在不更改任何代码的情况下注入真正的 Web 服务。
这是我的基本 Dart 页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dart Prototype</title>
<link rel="stylesheet" href="dart_prototype.css">
<link rel="import" href="location_container.html">
</head>
<body>
<h1>Dart Prototype</h1>
<div id="location_management_container">
<location-container location-service="{{applicationContext.locationService}}" count="{{startingCount}}"></location-container>
</div>
<script type="application/dart">
import 'package:web_ui/web_ui.dart';
import 'package:dart_prototype/dart_prototype_library.dart';
final ApplicationContext applicationContext = new ApplicationContext(
new WebService()
);
int startingCount = 5;
main() {
print('main initializing');
print(applicationContext);
print(applicationContext.locationService);
}
</script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
这是代码location-container
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<element name="location-container">
<template>
<div>
<ul id="todo-list">
<li>hi there!</li>
<li>{{count}}</li>
<li>{{locationService}}</li>
</ul>
</div>
</template>
<script type="application/dart">
import 'package:web_ui/web_ui.dart';
import 'package:dart_prototype/dart_prototype_library.dart';
class LocationContainer extends WebComponent {
@observable
WebService locationService;
@observable
int count;
LocationContainer() {
print('in loc container constructor');
print(locationService);
print(count);
}
created() {
print('created!');
print(locationService);
print(count);
}
}
</script>
</element>
</body>
</html>
这是 ApplicationContext 和 WebService 的代码
part of prototype_library;
class ApplicationContext {
final WebService locationService;
ApplicationContext(
this.locationService);
}
class WebService {
final Factory _objectFactory;
WebService(this._objectFactory);
Future call(String url) {
throw new UnimplementedError();
}
}
这是我打印到控制台的字符串语句的结果
Invalid CSS property name: -webkit-touch-callout
main initializing
Instance of 'ApplicationContext'
Instance of 'WebService'
in loc container constructor
null
null
created!
null
null
这是我呈现的页面返回的内容:
Dart Prototype
- hi there!
- 5
- Instance of 'WebService'
那个页面的来源......
<!DOCTYPE html>
<!-- This file was auto-generated from web/dart_prototype.html. -->
<html><head><style>template { display: none; }</style>
<meta charset="utf-8">
<title>Dart Prototype</title>
<link rel="stylesheet" href="../dart_prototype.css">
</head>
<body>
<h1>Dart Prototype</h1>
<div id="location_management_container">
<span is="location-container"></span>
</div>
<script type="application/dart" src="dart_prototype.html_bootstrap.dart"></script><script src="../packages/browser/dart.js"></script>
</body></html>