Christian Loitsch
Christian Loitsch

Reputation: 88

how do I @observe objects which do not implement observe.Observable in polymer

I use protobuffers for communication between my frontend end and my dart server.
Those objects do not implement Observable.

My dart-polymer object looks something like:

@CustomTag('user-address')
class UserAddress extends PolymerElement {

  @observable
  protobuffer.Address address = new protobuffer.Address();

My html-code looks something like:

<template>
  <form method="post" on-submit="{{doSubmit}}">
    <input type="text" value="{{ address.street }}" name="street">
    Your street is {{ address.street }}.

Because the protobuffer.Address doesn't implement Observable {{ address.street }} won't be updated.

I have found a few possible solutions / work-arounds, but I not happy with any of them:

[1] Those protobuffer objects are automatically generated by protobuf-builder which uses the dart-protoc-plugin.

Upvotes: 4

Views: 191

Answers (2)

Jonas Kello
Jonas Kello

Reputation: 1402

I know this may not be the answer you are looking for but I would really recommend that you make your data transfer object classes (the one you send across the wire) separate from your view objects classes (the ones supporting data-binding).

Having done a lot of MVVM pattern development I have found that this is always a good idea and worth the duplication that it creates. Forcing a single class to act both as a view object (data-binding to it) and as a data-transfer object (transferring it using eg. protofuf) usually just means a lot of trouble, especially as your application grows, the views become more complex, and the things you want to show on-screen will no longer map 1:1 with what you want to transfer.

Having separate classes with the single concern to support data binding will make your binding expressions much simpler. Because you can create properties freely on the view object without thinking about the data transfer, all your binding expressions will always be 1:1 to a property which makes troubleshooting data-bindings much simpler.

Upvotes: 1

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657466

I would discourage using Mock objects. They use noSuchMethod which uses reflection/mirrors which will probably cause troubles regarding code size on the client side.

The only alternative solution is see is to create wrapper classes that implement observable.

Upvotes: 0

Related Questions