Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML import: CustomTag not registered

I'm trying to create a custom element from polymer-element, but I cannot make the @CustomTag work. My dart file (my_element.dart) looks like this:

@HtmlImport('my_element.html')
library projects.projectFolder.layout;

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

@CustomTag('my-element')
class MyElement extends PolymerElement {

  @published String caption;
  MyElement.created() : super.created();
}

My html file (my_element.html) looks like this:

<link rel="import" href="../../../../packages/polymer/polymer.html">

<polymer-element name="my-element"> 
  <template>
    <link rel="stylesheet" href="my_element.css">

    <core-toolbar>
      <h1>{{ caption }}</h1>
    </core-toolbar>

  </template>
  <script type="application/dart" src="../my_element.dart"></script>
</polymer-element>

The thing is that the Chrome console keeps on printing the following error:

No elements registered in a while, but still waiting on 1 elements to be registered. Check that you have a class with an @CustomTag annotation for each of the following tags: 'my-element'.

It's curious, because I have declared the custom tag as it should be. It looks like it hasn't read the .dart file when it reaches the .html.
The .html file is called from another parent .html file. I think that the order in which the files are called could be the problem, but then again if Dart is compiled before running, it shouldn't care.
I have tried several solutions but none of them have worked. The only one that worked is pretty dirty one, which is importing the my_element.dart file straight from the main my_app.dart file. I guess it shouldn't be done that way because that would mean to import every single my_element.dart file in the same main my_app.dart.

EDIT (add index.html, pubspec.yml)
My index.html file looks like following:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
  <title>My app</title>
</head>
<body unresolved fullbleed>
  <my-app></my-app>

  <script type="application/dart">
    import 'package:polymer/polymer.dart';
    import 'package:project/my_app.dart';

    main() => initPolymer();

  </script>
</body>
</html>

my_app is another custom element, which is the main element where all the others go. In other words, it is like the main controller for the app. This element has a .dart and .html file as well, which will call other elements like my-element.
As you can see, my main() function is very simple, since it only init Polymer (see index.html).

My pubspec.yml looks like following:

name: project
version: 0.0.1
description: Some project.
author: Tomas
homepage: https://github.com/*******

environment:
  sdk: '>=1.10.0 <2.0.0'

dependencies:
  polymer: '^0.16.0'
  core_elements: '^0.7.1'
  paper_elements: '^0.7.1'
  route_hierarchical: "^0.6.1"

transformers:
- polymer:
    entry_points: web/index.html
- $dart2js:
    $include: "**/*.polymer.bootstrap.dart"

My directory looks like this (I just show a fraction of it, the important one I hope):

.
├── README.md
├── build
│   └── web
│       ├── index.html
│       ├── index.html.polymer.bootstrap.dart.js
│       └── packages
├── lib
│   ├── my_app.dart
│   └── src
│       ├── elements
│       ├── layout
│       │   ├── my_element.css
│       │   ├── my_element.dart
│       │   └── my_element.html
│       ├── my_app.css
│       ├── my_app.html
│       └── modules
│           └── module.dart
│       ├── my_app.css
├── pubspec.lock
├── pubspec.yaml
└── web
│   ├── index.html
│   └── packages -> ../packages
│       ├── my_app.html
│       └── modules
│           └── module.dart

I hope that this edit doesn't confuse more than it should. I'm just looking to know which is the correct folder structure and import form of a Dart app.

In short terms: how should the tree look like when programming a big application in dart, and where should I do the imports? I have look at every documentation I could, including some tutorials, but all of them talk about very simple examples where the big part of the code is in the web folder, which I wouldn't want.

EDIT (summarize and rephrase, add my_app.html and my_app.dart)

In fewer words:

I've a custom element defined by my_element.html and my_element.dart (defined above in this answer) and I want to import it into another my_app element using only the html. I.e. not by importing my_element.dart in my_app.dart, but by only importing my_element.html using a link tag in my_app.html:

<link rel="import" href="../../../packages/polymer/polymer.html">
<link rel="import" href="layout/my_element.html">

<polymer-element name="my-app">
  <template>

    <core-scaffold id="scaffold">

      <my-element tool flex></my-element>

      <main fit></main>

    </core-scaffold>

  </template>
  <script type="application/dart" src="../my_app.dart"></script>
</polymer-element>

my_app.dart:

@HtmlImport('src/my_app.html')

import 'package:polymer/polymer.dart';
import 'package:core_elements/core_scaffold.dart';

@CustomTag('my-app')
class MyApp extends PolymerElement {

  MyApp.created() : super.created();
}

Shouldn't the <script type="application/dart" src="../my_element.dart"></script> be enough to tell the compiler to load that dart file for registering the element tag by following the transitive dependence defined by the html import?

like image 547
tomasyany Avatar asked May 18 '15 21:05

tomasyany


1 Answers

You need to import all your custom component Dart classes in your my_app.dart. Typically you'd create a single Dart file to import, which would export each entry so that your my_app.dart stays clean.

like image 133
Daedalus Avatar answered Nov 01 '22 16:11

Daedalus