Callbacks and extends

90 views
Skip to first unread message

Julien Eluard

unread,
Feb 17, 2014, 9:01:22 AM2/17/14
to polym...@googlegroups.com
Hi,

testing the 'extends' feature I was surprised to see that callbacks from super elements are not called when overridden. Super callbacks are called when no overrides are provided. Note that calling this.super() does what is expected (see here: http://jsfiddle.net/29btP/)
I had a similar behavior when testing directly with Custom Element.

Is that expected? I find it a little counterintuitive.

Thanks,
Julien

Scott Miles

unread,
Feb 17, 2014, 1:16:41 PM2/17/14
to Julien Eluard, polymer-dev
> callbacks from super elements are not called when overridden

This is normal for JavaScript inheritance chains. Any method (including callbacks) resolves to the most-derived (subclass) implementation. The subclass gets the choice of when/how/if to call a superclass method.

I.e., if x-foo has a method 'doStuff', and x-bar extends x-foo, then x-bar can override doStuff and:
  • completely redefine doStuff behavior by reimplementing and never calling x-foo.prototype.doStuff
  • do some work, then call this.super() to get the default behavior, then do more work
  • capture the return value of this.super() and modify or act on it
  • control arguments sent to x-foo implementation via this.super() (note that super takes an _array_ of pass-through arguments as a parameter, e.g. `this.super(arguments)` or `this.super([a, 3, null]))
If you automate calling super-class methods, you lose all the flexibility above. This is why we jump through several hoops to provide `this.super` method.


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/a94807a6-88c5-480d-b2d1-be3c6ee290f2%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Julien Eluard

unread,
Feb 17, 2014, 2:16:07 PM2/17/14
to Scott Miles, polymer-dev
Thanks!

I was assuming callbacks would get some special treatment, especially 'created'.
Now it certainly makes sense to provide some flexibility to the user.

Julien
Reply all
Reply to author
Forward
0 new messages