| OLD | NEW |
| (Empty) | |
| 1 Frameworks |
| 2 ---------- |
| 3 |
| 4 Sky is intended to support multiple frameworks. Here is one way you |
| 5 could register a custom element using Dart annotations: |
| 6 |
| 7 ```dart |
| 8 // @tagname annotation for registering elements |
| 9 // only useful when placed on classes that inherit from Element |
| 10 class tagname extends AutomaticMetadata { |
| 11 const tagname(this.name); |
| 12 final String name; |
| 13 void init(DeclarationMirror target, Module module, ScriptElement script) { |
| 14 assert(target is ClassMirror); |
| 15 if (!(target as ClassMirror).isSubclassOf(reflectClass(Element))) |
| 16 throw new UnsupportedError('@tagname can only be used on descendants of El
ement'); |
| 17 module.registerElement(name, (target as ClassMirror).reflectedType); |
| 18 } |
| 19 } |
| 20 ``` |
| 21 |
| 22 A framework that used the above code could use the following code to |
| 23 get the tag name of an element: |
| 24 |
| 25 ```dart |
| 26 String getTagName(Element element) { // O(N) in number of annotations on the cla
ss |
| 27 // throws a StateError if the class doesn't have an @tagname annotation |
| 28 var tagnameClass = reflectClass(tagname); |
| 29 return (reflectClass(element.runtimeType).metadata.singleWhere((mirror) => mir
ror.type == tagnameClass).reflectee as tagname).name; |
| 30 } |
| 31 ``` |
| OLD | NEW |