richard
richard

Reputation: 3319

Dart Polymer - passing published variable to other classes

A humble view pattern increases the unit test coverage of an GUI application by splitting a GUI class into a View and a Presenter. If done properly the View becomes so simple (humble) that it does not require any testing, and the Presenter only contains normal (non GUI) field that are easy to test or mock.

(see http://martinfowler.com/eaaDev/uiArchs.html towards the bottom of the page for more information on this pattern).

I am trying to apply the 'humble view' pattern to a polymer application and thought I would use the simplest approach of 'injecting' the published (observed) fields into my Presenter class. But I have found that when the Presenter changes the value of a published field the GUI is not updated.

Here is an example based on the clickcounter code. As you can see the count field is injected into the Presenter and the method increment() is called when the button is clicked, but the changed value is not reflected in the GUI

Any idea why this is not working?

@CustomTag('click-counter')
class ClickCounter extends PolymerElement {
  @published int count = 0;

  MyTestedClass mytestedclass;

  ClickCounter.created() : super.created() {
    mytestedclass = new MyTestedClass()..count = count;
  }

  void increment() {
    mytestedclass.increment();
  }
}

class MyTestedClass{

  int count = 0;

  void increment() {
    count++;
  }
}

One possible solution is to define an interface to bind the two instances together. However it does appear to be a bit 'clunky'

class View {
  int count;
}

@CustomTag('click-counter')
class ClickCounter extends PolymerElement implements View{
  @published int count = 0;

  MyTestedClass mytestedclass;

  ClickCounter.created() : super.created() {
    mytestedclass = new MyTestedClass( this);
  }

  void increment() {
    mytestedclass.increment();
  }
  void setCount( int count){
    this.count = count;
  }
}

class MyTestedClass{

  View view;

  MyTestedClass( this.view);

  void increment() {
    view.count++;
  }
}

Upvotes: 0

Views: 170

Answers (1)

Justin Fagnani
Justin Fagnani

Reputation: 11201

You need an @observable annotation on MyTestedClass.count so that changes to it are published.

One day that annotation might not be required, but for now it is.

Upvotes: 2

Related Questions