Any way to have Namespace'd Constructors

148 views
Skip to first unread message

Vicențiu Ciorbaru

unread,
Aug 11, 2014, 4:35:10 PM8/11/14
to polym...@googlegroups.com
Hi polymer devs,

I've recently been involved in porting the trace-viewer[1] project bundled with Google Chrome to use Polymer.

Given the size of the project, and the fact that it's mostly just one big page, we've run into the need to use namespaces for our modules. In order to avoid conflicts we've attempted to implement the rule that each constructor for a polymer element should have a name like:

/path/to/file + ElementConstructor => PathToFileElementConstructor

Unfortunately, this quickly gets out of hand and has a major readability impact in the code. An example is a polymer element defined in the file /tracing/analysis/analysis-view.html. This ends up being TracingAnalysisAnalysisView. What would be really useful is to be able to export the constructor of the element within a namespace, and not have it stuck to the window object.

I'm looking for something along the lines of:
<polymer-element name="foo-element" constructor="FooElement" namespace="foo.space">
<style>
/* css */
</style>
<template>
<!-- More HTML -->
</template>

<script>
  Polymer({
    ready
: function(){
     
this.barProperty = [];
   
}
 
});
</script>
</polymer-element>

/* some other file */
<link rel="import" href="foo/space/foo_element.html">

<script>
var fooElement = new foo.space.FooElement();
</script>


I've read the available documentation on this, but I could not find an appropriate way to actually solve this issue. Is there any trick available to solve this?

Regards,
Vicențiu Ciorbaru

Steve Orvell

unread,
Aug 11, 2014, 5:06:40 PM8/11/14
to Vicențiu Ciorbaru, polym...@googlegroups.com
I suggest filing an issue here: https://github.com/Polymer/polymer-dev/issues.


Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/ab9aa813-266e-4287-916e-c2595cffcfbc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vicențiu Ciorbaru

unread,
Aug 11, 2014, 5:16:41 PM8/11/14
to polym...@googlegroups.com, cvic...@chromium.org
I've opened an issue there to a link to this thread. It would be helpful to have any potential workaround ideas in the meantime, if you have any.

Vicențiu

Scott Miles

unread,
Aug 13, 2014, 8:57:56 PM8/13/14
to Vicențiu Ciorbaru, polymer-dev
One possibility is to make a custom constructor:

<script>
  foo.space.FooElement = function() {
    return document.createElement('foo-element');
  }
</script>

you can also use this technique to make constructors that take arguments.


Rob Dodson

unread,
Aug 13, 2014, 9:33:41 PM8/13/14
to Scott Miles, Vicențiu Ciorbaru, polymer-dev

ulyb...@gmail.com

unread,
Aug 29, 2014, 12:37:43 PM8/29/14
to polym...@googlegroups.com, cvic...@chromium.org
Thanks for the tip !

I'm having trouble to make it work with elements that extends Native HTML elements.

I noticed that when using the `constructor` attribute in polymer-element, it will smartly returns a fully working element (let's say FooElement extends button) :
<button is="foo-element">...</button>


So I tweaked your function like this : (let's say FooElement extends button) 

foo.space.FooElement = function() {
 
var el = document.createElement('button');
   el
.setAttribute('is','foo-element')
 
return el;
}

which returns in appearance the same thing as the constructor defined as attribute, but that is in fact just a simple button with a dummy "is" attribute.

Any ideas on how to make namespace constructor of elements that extend native Html ?

Thanks

Steve Orvell

unread,
Aug 29, 2014, 12:43:54 PM8/29/14
to ulyb...@gmail.com, polym...@googlegroups.com, Vicențiu Ciorbaru
Custom elements that use the `is` syntax to extend native elements must be created imperatively like this:

    document.createElement('button', 'foo-element');



ulyb...@gmail.com

unread,
Aug 29, 2014, 1:16:19 PM8/29/14
to polym...@googlegroups.com, ulyb...@gmail.com, cvic...@chromium.org
That's perfect, thanks !
Reply all
Reply to author
Forward
0 new messages