如何在客户端和服务器上与Polymer共享数据结构 [英] How to share data structures with Polymer on both client and server

查看:136
本文介绍了如何在客户端和服务器上与Polymer共享数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:
我有一个dart文件定义一些数据结构,我需要为客户端和服务器使用。我想用聚合物来观察这些数据结构。但是,由于Polymer,服务器不能包括该文件,因为Polymer包括 dart:html

上下文:
我在客户端/服务器(REST-full)应用程序上工作,其中我希望服务器提供定义为资源的数据结构。客户端应显示这些资源,并有可能将修改发送到服务器。为此,Polymer是非常宝贵的。

Context: I am working on a client/server (REST-full) application, where I want the server to provide the data structures defined available as resources. The client should display these resources, and have the possibility to send the modifications to the server. For that, Polymer is invaluable.

我想让这个库可用于服务器的原因是我希望服务器能够验证要存储的资源。

The reason I want to have this library available for the server is that I want the server to be able to validate the resources to be stored.

可能的解决方案:

我还不知道聚合物足够了,但如果我的数据结构可以继承 Map ,我可以在客户端代码中使用 toObservable 数据结构可观察,但是不是通过点符号访问,我不得不通过键来访问成员,使其相当脆弱。

I don't yet know the internals of Polymer enough, but if my data structures could inherit from Map, I could use toObservable in the client side code to make the data structure observable, but instead of accessing by dot notation, I'd have to access members by keys instead, making it rather fragile.

我想知道如果我能使用 mirrors.dart 在客户端上添加 observable 注释。

I was wondering if I could use mirrors.dart to add the observable annotation on the client.

当然,管理重复的代码真的不是解决方案。

Of course, managing duplicate code, is really not a solution.

推荐答案

c>观察包。

You can use the observe package.

通过 ChangeNotifier ,您可以通过调用 notifyPropertyChange 当值改变时。

With ChangeNotifier you initiate the change notification yourself by calling notifyPropertyChange when a value changes. The changes get delivered synchronously.

可观察需要 dirtyCheck()

Observable needs dirtyCheck() to be called to deliver changes.

重复调用 Observable.dirtyCheck()自动获得更改。

Polymer calls Observable.dirtyCheck() repeatedly to get the changes automatically.

每个的示例

import 'package:observe/observe.dart';

class Notifiable extends Object with ChangeNotifier {
  String _input = '';

  @reflectable
  get input => _input;

  @reflectable
  set input(val) {
    _input = notifyPropertyChange(#input, _input, val + " new");
  }

  Notifiable() {
    this.changes.listen((List<ChangeRecord> record) => record.forEach(print));
  }
}

class MyObservable extends Observable {
  @observable
  String counter = '';

  MyObservable() {
    this.changes.listen((List<ChangeRecord> record) => record.forEach(print));
  }
}

void main() {
  var x = new MyObservable();
  x.counter = "hallo";
  Observable.dirtyCheck();

  Notifiable notifiable = new Notifiable();
  notifiable.input = 'xxx';
  notifiable.input = 'yyy';
}

这篇关于如何在客户端和服务器上与Polymer共享数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆