dart-archive / polymer-dart

Polymer support for Dart
https://pub.dartlang.org/packages/polymer
BSD 3-Clause "New" or "Revised" License
180 stars 33 forks source link

add [] operator to DomBindModel, and deprecate the item getter #655

Closed jakemac53 closed 8 years ago

jakemac53 commented 8 years ago

Fixes https://github.com/dart-lang/polymer-dart/issues/654

sigmundch commented 8 years ago

lgtm

terrywarwar commented 8 years ago

Do we still use var model = new DomRepeatModel.fromEvent(e) with on-click events. I'm getting the following error. `Uncaught Unhandled exception: type 'IronList' is not a subtype of type 'TemplateElement' in type cast.

0 Object._as (dart:core-patch/object_patch.dart:70)

1 _getItemName (package:polymer/src/template/dom_repeat.dart:152:41)

2 DomRepeatModel.DomRepeatModel (package:polymer/src/template/dom_repeat.dart:140:25)

3 DomRepeatModel.DomRepeatModel.fromEvent (package:polymer/src/template/dom_repeat.dart:147:16)

4 ListComp.onClicked (package:xxx/components/fyyyy_list.dart:87:21)

5 Function._apply (dart:core-patch/function_patch.dart:7)

6 Function.apply (dart:core-patch/function_patch.dart:28)

7 _InstanceMirrorImpl.invoke (package:reflectable/src/reflectable_transformer_based.dart:198:23)

8 _setupReflectableMethods.. (package:polymer/src/common/polymer_descriptor.dart:162:31)`

jakemac53 commented 8 years ago

interesting, can you post a repro?

terrywarwar commented 8 years ago

I wonder if it's the iron-list I'm using:

<iron-list items="[[data]]" as="item">
                    <template>

                        <div class="cards">
                            <paper-card>
                                <div class="card-content" >
                                    <div class="horizontal layout">
                                        <div>
                                            Date: <span style="font-weight: bold">[[formatDate(item.createdDate)]]</span>
                                        </div>
                                        <div style="padding-left: 20px">
                                            Created By: <span style="font-weight: bold">[[item.createdBy]]</span>
                                        </div>
                                    </div>
                                    <div>
                                        <p>[[item.description]]</p>
                                    </div>

                                </div>
                                <div class="card-actions">
                                    <paper-icon-button icon="chrome-reader-mode" title="Details" on-click="onShutdownClicked"></paper-icon-button>

                                </div>
                            </paper-card>
                        </div>
                    </template>
                </iron-list>
  void onShutdownClicked(Event e, [_]){
    var model = new DomRepeatModel.fromEvent(e);
set('selectedShutdownId', model.item['id']);
}```
terrywarwar commented 8 years ago

It was working with rc.8.

dam0vm3nt commented 8 years ago

Is it allowed to use DomRepeatModel when not inside a dom-repeat ? I thought not.

dam0vm3nt commented 8 years ago

@jakemac53 could it be because of this ? Chances are that removing the type it will work with iron-list too ?

dam0vm3nt commented 8 years ago

BTW @jakemac53 : probably index too should be treated in a similar way of itemwhen using indexAs. Also what will happen when we have nested dom-repeat ? Would be nice to have all the items and indexes too, or a hierarchy of models...

jakemac53 commented 8 years ago

Ya, it looks like you are using iron-list not dom-repeat, so that is the issue. It was working before only by accident. You could use probably use IronList.modelForElement instead (with the event.target)?

terrywarwar commented 8 years ago

I tried

Element el = e.target as Element;
var model = ($['itemList'] as IronList).modelForElement(el);
print(model.index);

but keep getting the following error

Uncaught Unhandled exception: Class 'TemplateInstance' has no instance getter 'index'.

example shows:

  /// Example:
  ///
  ///   var model = modelForElement(el);
  ///   if (model.index < 10) {
  ///     model.set('item.checked', true);
  ///   }
terrywarwar commented 8 years ago

Found this and changed it to model.get('item.id') which worked. http://stackoverflow.com/questions/33640160/how-to-get-model-for-element-in-iron-list-polymer-dart

Thanks.