js_util 0.2.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 0

js_util

Build Status

Utilities to access JavaScript from Dart.

  • toJS(o): Converts a Dart object to a JavaScript object.
  • newObject(): Creates a new JavaScript object.
  • defineProperty(o, String prop, PropertyDescription desc): A wrapper for Object.defineProperty
  • getValue(o, String prop): Returns o[prop].
  • setValue(o, String prop, value): Performs o[prop] = value.

These utilities are of great help if a JavaScript API takes a JavaScript object with keys that are not fixed because js package does not let you create a JavaScript object without declaring a Dart class with @JS() and @anonymous.

Getting Started

Include js_util.js in index.html to use js_util functions.

<html>
  <head>
    <script async src="packages/js_util/dist/js_util.js"></script>
    <script async src="packages/browser/dart.js"></script>
  </head>
</html>

Usage

A simple usage example:

final obj = newObject();
defineProperty(obj, 'foo', new PropertyDescription(enumerable: true, value: 1));
defineProperty(obj, 'bar', new PropertyDescription(enumerable: false, value: 2));
final obj = newObject();
setValue(obj, 'foo', 1);
setValue(obj, 'bar', 2);

print(getValue(obj, 'foo')); // 1
print(getValue(obj, 'bar')); // 2
final jsObj = toJS({
  'people': [
    {'firstName': 'Kwang Yul', 'lastName': 'Seo'},
    {'firstName': 'DoHyung', 'lastName': 'Kim'},
    {'firstName': 'Kyusun', 'lastName': 'Kim'}
  ]
});

final people = getValue(jsObj, 'people');
print(getValue(people[0], 'firstName')); // 'Kwang Yul'

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

0.2.0

  • Add @JS annotations on library directives to support 1.16.0
  • Add the minified version of js_util.js

0.1.0

  • Initial version

example/js_util.dart

import 'dart:html';

import 'package:js_util/js_util.dart';

void main() {
  final jsObj = toJS({
    'people': [
      {'firstName': 'Kwang Yul', 'lastName': 'Seo'},
      {'firstName': 'DoHyung', 'lastName': 'Kim'},
      {'firstName': 'Kyusun', 'lastName': 'Kim'}
    ]
  });

  final people = getValue(jsObj, 'people');
  final firstPerson = people[0];
  final text =
      '${getValue(firstPerson, "firstName")} ${getValue(firstPerson, "lastName")}';
  document.querySelector('#container').innerHtml = text;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  js_util: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:js_util/js_util.dart';
  
Version Uploaded Documentation Archive
0.2.0 May 2, 2016 Go to the documentation of js_util 0.2.0 Download js_util 0.2.0 archive
0.1.0 Mar 30, 2016 Go to the documentation of js_util 0.1.0 Download js_util 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
0
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0