Discussion:
[dart-misc] exposing namespaces to JS
Peter StJ
2016-02-29 23:36:58 UTC
Permalink
I am looking for a way to expose namespaced functions and possibly instance
methods/properties.

for example like how google.maps.* works.

The way this works in closure (from where maps are exposed) is that the
namespace is created specifically named as desired while the method names
are possibly as they are in the class implementation, something in the
lines of:

goog.exportMethod('my.namespace.fnname', /* actual code that will be
compiled / minified */(...));

class MyClass {
/** @export */
myMethod() {...} // name will be preserved as it is so accessible when an
instance of the (possibly renamed) class is created
}

The main idea is that I want to be able to expose entry points to Dart code
while Dart is compiled as a whole application, for example provide runtime
settings or API that can change behavior to the browser users or possibly
scriptwriters / extension writers.

Is this possible now in latest Dart? If yes - where are the relevant docs?

Thank you.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Kun Zhao
2016-03-02 14:51:45 UTC
Permalink
You can do
import 'dart:js'

var my = context['my'] = new JsObject(context['object'], []);
var namespace = my['namespace'] = new JsObject(context['object'], []);
namespace['fname'] = () {
// dart function implementation.
}


in javascript, my is a global object. you can call the method:

my.namespace.fname()

I am not sure how to do this in the new JS package. The document does not
mention how to create a JS object.
Post by Peter StJ
I am looking for a way to expose namespaced functions and possibly
instance methods/properties.
for example like how google.maps.* works.
The way this works in closure (from where maps are exposed) is that the
namespace is created specifically named as desired while the method names
are possibly as they are in the class implementation, something in the
goog.exportMethod('my.namespace.fnname', /* actual code that will be
compiled / minified */(...));
class MyClass {
myMethod() {...} // name will be preserved as it is so accessible when
an instance of the (possibly renamed) class is created
}
The main idea is that I want to be able to expose entry points to Dart
code while Dart is compiled as a whole application, for example provide
runtime settings or API that can change behavior to the browser users or
possibly scriptwriters / extension writers.
Is this possible now in latest Dart? If yes - where are the relevant docs?
Thank you.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Kun Zhao
2016-03-02 14:54:56 UTC
Permalink
I wrote a jsproxy just to do that. It can also add set/get method.
Post by Kun Zhao
You can do
import 'dart:js'
var my = context['my'] = new JsObject(context['object'], []);
var namespace = my['namespace'] = new JsObject(context['object'], []);
namespace['fname'] = () {
// dart function implementation.
}
my.namespace.fname()
I am not sure how to do this in the new JS package. The document does not
mention how to create a JS object.
Post by Peter StJ
I am looking for a way to expose namespaced functions and possibly
instance methods/properties.
for example like how google.maps.* works.
The way this works in closure (from where maps are exposed) is that the
namespace is created specifically named as desired while the method names
are possibly as they are in the class implementation, something in the
goog.exportMethod('my.namespace.fnname', /* actual code that will be
compiled / minified */(...));
class MyClass {
myMethod() {...} // name will be preserved as it is so accessible when
an instance of the (possibly renamed) class is created
}
The main idea is that I want to be able to expose entry points to Dart
code while Dart is compiled as a whole application, for example provide
runtime settings or API that can change behavior to the browser users or
possibly scriptwriters / extension writers.
Is this possible now in latest Dart? If yes - where are the relevant docs?
Thank you.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Loading...