3

can someone tell me some infos about @observable @published in combination with toObservable

imagine i have a .dart file that has a

@observable List array = toObservable([]);

that array will be passed down to another polyview.

so should i write

@published List array;

or

@published List array = toObservable([]);

would I have to repeat the toObservable on published values that already have been initialized as toObservable ...

like having a complex object with 3 to 4 levels

  migrationview            observable migration 
   - actionsview           published actions -> migration.actions
    -createTableview       published createTable -> actions.createTables
     -tableview            published table -> createTables.table
      -columnview          published columns -> table.columns

I want to be sure that the changes made in the column view eg add a list to the columns will be recognize by the observable migration object ..

whats the correct way for doing something .. and specially when using classes

and should I only specify maps and lists as toObservable() what about classed

Migration migration = toObservable(new Migration()); // ???????

I already defined lists and maps inside migration as @observable

H.R.
  • 496
  • 4
  • 14

1 Answers1

2

It is enough to call toObservable([]) once on a list or map, even when you pass it around.

When you assign the observable collection to a field and use this field in a binding expression you need to make the field observable too (as you did with the @published annotation.
Making the list itself observable is only to notify Polymer about additions/removals in the list but Polymer also needs to know that there was something new assigned that needs to be reflected in the view.

If you have custom model classes you want to use in Polymer binding expressions use the Observable mixin and add the @observable annotation.
For more details see Implement an Observer pattern in Dart

extends PolymerElement already contains the Observable mixin.
@published implies @observable

If you create a new list you need of course to make it observable to get change notifications.

@published List array = toObservable([1,2,3,4]);

If you always create a new list and never change an existing one you don't need toObservable([]) at all.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567