3

Im building an app in dart using polymer.dart, and im realising i need some way to communicate between my Polymer elements. I've set my eyes upon event_bus and i am trying to make it work with polymer.

It seems however that when i try to put my PolymerElement class into my_lib i get the following error:

line 1 pos 6: url expected
part of my_lib;
     ^

I get that it seems like every PolymerElement should be self contained, but im having a hard time figuring out how i would handle communication from one PolymerElement to another.

So in short, what i would like to know is how do i put my elements in the same library so they can share an eventbus or what is the prefered way to handle communication between Polymer elements?

I can't seem to find any examples of communication between PolymerElements, so pointers to documentation or examples would be appreciated.

CoolMcGrrr
  • 774
  • 8
  • 21

1 Answers1

3

You are right, it's best to have just one Polymer element in one library and nothing else.

I use PolymerElements and EventBus.
This is no problem. You don't have to put all classes in the same library to use them. Just import what you need.

If you have the files you want to import in your packages lib directory import them like they were in some dependent package.

import 'package:yourpackagename/file_to_import.dart';
import 'package:yourpackagename/src/file_to_import.dart'; // just to show that other paths work too
import 'package:yourpackagename/src/someotherdir/file_to_import.dart';  // - " -
import 'package:yourpackagename/anotherdir/someotherdir/file_to_import.dart'; // - " -

If the file you want to import is in the web directory of your application package or any subdirectory of web use relative paths like

import 'file_to_import.dart'
import 'src/file_to_import.dart'; // just to show that other paths work too
import 'someotherdir/file_to_import.dart';  // - " -
import 'anotherdir/someotherdir/file_to_import.dart'; // - " -

After importing a library you can access all non-private classes/functions/variables like they were in the same library.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    I'm amazed i didn't think of this. It's such a simple solution! :) Great answer! – CoolMcGrrr Jan 23 '14 at 19:55
  • @Günter Zöchbauer which EventBus are you using? – Fedy2 Sep 03 '14 at 13:25
  • I started with https://pub.dartlang.org/packages/event_bus. I customize it often for my own needs. https://github.com/bwu-dart/bwu_datagrid/blob/master/lib/core/event_bus.dart – Günter Zöchbauer Sep 03 '14 at 13:30
  • You can also take a look at http://stackoverflow.com/questions/25593981/use-a-class-as-attribute-for-a-dart-polymer-element/25594368#25594368 Here you have just a polymer-element that provides services by listening to events. I used a similar approach in http://stackoverflow.com/a/25256858/217408 (bwu_polymer_routing) – Günter Zöchbauer Sep 03 '14 at 13:38