当动态添加dart聚合物元素时,如何在DOM上获取可观察的变量 [英] When dynamically adding dart polymer elements, how do I get observable variables to on DOM

查看:119
本文介绍了当动态添加dart聚合物元素时,如何在DOM上获取可观察的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改使用聚合库的默认网络应用程序,以便从DART代码创建并添加到DOM中的聚合元素,而不是包含在HTML中。我已经成功地将元素添加到DOM,但我的observable变量不是在DOM上更新。事件被触发,值正在改变。我有使用Observable.dirtyCheck()更新的DOM,但是,这显然是昂贵的,所以我试图找出如何获取聚合物更新dom没有dirtyCheck()。

I am trying to change the default web application that uses the polymer library so that the polymer element is created and added to the DOM from DART code rather than including in the HTML. I have succeeded in adding the element to the DOM, but my observable variable are not being updated on the DOM. The events are being fired, and the values are changing. I have got the DOM to update using Observable.dirtyCheck(), however, this is apparently expensive, so am trying to figure out how to get polymer to update dom without dirtyCheck().

所以,总之,如何摆脱Observable.dirtyCheck()?

So, In short, how to I get rid of Observable.dirtyCheck()???

dynamiccreate.dart p>

dynamiccreate.dart

library dynamiccreate;

import 'dart:html';
import 'package:polymer/polymer.dart';



main() {
  initPolymer();

  //create click-counter element at runtime from DART, not HTML
  var NewElement = new Element.tag('click-counter');
  NewElement.setAttribute("count", "5");
  //Add to DOM
  querySelector('#sample_container_id').children.add(NewElement);
}

dynamiccreate.html
$ b

dynamiccreate.html

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <title>Sample app</title>
    <link rel="stylesheet" href="dynamiccreate.css">

    <!-- import the click-counter -->
    <link rel="import" href="clickcounter.html">
    <!-- <script type="application/dart">export 'package:polymer/init.dart';</script> -->
    <script src="packages/browser/dart.js"></script>
  </head>
  <body>
    <h1>DynamicCreate</h1>

    <p>Hello world from Dart!</p>

    <div id="sample_container_id">
     <!--  <click-counter count="5"></click-counter> -->
    </div>
    <script src="dynamiccreate.dart" type="application/dart"></script>
  </body>
</html>

clickcounter.dart

import 'package:polymer/polymer.dart';

/**
 * A Polymer click counter element.
 */
@CustomTag('click-counter')
class ClickCounter extends PolymerElement {
  @published int count = 0;

  ClickCounter.created() : super.created() {
  }

  //increment gets called when dynamically adding object at runtime
  //But does not update count on DOM
  void increment() {
    count++;
    //Have to add this to update count in DOM
    Observable.dirtyCheck(); //<<<---How do I get rid of this???
  }
}

clickcounter.html / p>

clickcounter.html

<polymer-element name="click-counter" attributes="count">
  <template>
    <style>
      div {
        font-size: 24pt;
        text-align: center;
        margin-top: 140px;
      }
      button {
        font-size: 24pt;
        margin-bottom: 20px;
      }
    </style>
    <div>
      <button on-click="{{increment}}">Click me</button><br>
      <span>(click count: {{count}})</span>
    </div>
  </template>
  <script type="application/dart" src="clickcounter.dart"></script>
</polymer-element>


推荐答案

更改您的dynamiccreate.dart文件,并且计数器在UI中开始递增:

Change your dynamiccreate.dart file to look like this, and the counter starts incrementing in the UI:

library dynamiccreate;

import 'dart:html';
import 'package:polymer/polymer.dart';

main() {
  initPolymer().run(() {
    var newElement = new Element.tag('click-counter');
    newElement.setAttribute("count", "15");
    querySelector('#sample_container_id').children.add(newElement);
  });
}

Nit:命名您的变量 newElement ,而不是 NewElement 。固定在这里。

Nit: name your variable newElement, not NewElement. Fixed here.

这篇关于当动态添加dart聚合物元素时,如何在DOM上获取可观察的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆