OLD | NEW |
(Empty) | |
| 1 # js_util |
| 2 |
| 3 [![Build Status](https://travis-ci.org/fluidic/js_util.svg?branch=master)](https
://travis-ci.org/fluidic/js_util) |
| 4 |
| 5 Utilities to access JavaScript from Dart. |
| 6 |
| 7 * toJS(o): Converts a Dart object to a JavaScript object. |
| 8 * newObject(): Creates a new JavaScript object. |
| 9 * defineProperty(o, String prop, PropertyDescription desc): A wrapper for [Objec
t.defineProperty][defineProperty] |
| 10 * getValue(o, String prop): Returns `o[prop]`. |
| 11 * setValue(o, String prop, value): Performs `o[prop] = value`. |
| 12 |
| 13 These utilities are of great help if a JavaScript API takes a JavaScript object |
| 14 with keys that are not fixed because [js][js] package does not let you create a |
| 15 JavaScript object without declaring a Dart class with `@JS()` and `@anonymous`. |
| 16 |
| 17 [defineProperty]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refere
nce/Global_Objects/Object/defineProperty |
| 18 [js]: https://pub.dartlang.org/packages/js |
| 19 |
| 20 ## Getting Started |
| 21 |
| 22 Include `js_util.js` in index.html to use js_util functions. |
| 23 |
| 24 ```html |
| 25 <html> |
| 26 <head> |
| 27 <script async src="packages/js_util/dist/js_util.js"></script> |
| 28 <script async src="packages/browser/dart.js"></script> |
| 29 </head> |
| 30 </html> |
| 31 ``` |
| 32 |
| 33 ## Usage |
| 34 |
| 35 A simple usage example: |
| 36 |
| 37 ```dart |
| 38 final obj = newObject(); |
| 39 defineProperty(obj, 'foo', new PropertyDescription(enumerable: true, value: 1)); |
| 40 defineProperty(obj, 'bar', new PropertyDescription(enumerable: false, value: 2))
; |
| 41 ``` |
| 42 |
| 43 ```dart |
| 44 final obj = newObject(); |
| 45 setValue(obj, 'foo', 1); |
| 46 setValue(obj, 'bar', 2); |
| 47 |
| 48 print(getValue(obj, 'foo')); // 1 |
| 49 print(getValue(obj, 'bar')); // 2 |
| 50 ``` |
| 51 |
| 52 ```dart |
| 53 final jsObj = toJS({ |
| 54 'people': [ |
| 55 {'firstName': 'Kwang Yul', 'lastName': 'Seo'}, |
| 56 {'firstName': 'DoHyung', 'lastName': 'Kim'}, |
| 57 {'firstName': 'Kyusun', 'lastName': 'Kim'} |
| 58 ] |
| 59 }); |
| 60 |
| 61 final people = getValue(jsObj, 'people'); |
| 62 print(getValue(people[0], 'firstName')); // 'Kwang Yul' |
| 63 ``` |
| 64 |
| 65 ## Features and bugs |
| 66 |
| 67 Please file feature requests and bugs at the [issue tracker][tracker]. |
| 68 |
| 69 [tracker]: https://github.com/ProtoCatTeam/js_util/issues |
OLD | NEW |