Polymer Dart $[] selector in an autobinding model

后端 未结 1 1043
庸人自扰
庸人自扰 2021-01-24 03:52

Since polymer-body has been removed, we need to use an auto-binded template to use polymer binding features outside of a PolymerElement

1条回答
  •  南笙
    南笙 (楼主)
    2021-01-24 04:34

    I would suggest to use a normal Polymer element instead of auto-binding-dart.
    Then you don't have to care about differences and you need no 'main'. I always start a Polymer project with an Polymer element that acts as main() and container for the entire app.

    I also would suggest to not use inline code.
    As far as I know it has some limitations especially debugging is not supported (might be fixed already, I don't know because I never use it).

    To make $ work you need a small and simple workaround;

    import 'dart:html';
    import 'package:polymer/polymer.dart';
    import 'package:template_binding/template_binding.dart';
    
    
    Map $; // we define our own '$'
    
    main() {
      initPolymer().run(() {
        Polymer.onReady.then((_) {
          var template = document.querySelector('template') as Polymer;
          $ = template.$;             // we assign template.$ to our own '$' so we can omit the 'template' part
          templateBind(template).model = new MyModel();
        });
      });
    }
    
    class MyModel extends Observable {
      //$['mybutton'] wont work there - it can't because this is outside of a method
      @observable String value = 'something';
      buttonTap() {
        print($['mybutton'].id); // here it works
        print('tap!');
      }
    }
    

    0 讨论(0)
提交回复
热议问题