Namespaces: New themes in modules should use Drupal.theme.prototype
| Project: | JavaScript Theming |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
I think it is the case that when a module creates a new theme, it should go in Drupal.theme.prototype instead of in Drupal.theme.
The rational for the abuse of the prototype object by the theming engine is that it makes it possible to override a theming function somewhere else. For example, if I want to override 'table' in my theme, I would create Drupal.theme.table(). The original table function would still exist in Drupal.theme.prototype.table(), but based on the Drupal.theme() logic, my custom one would be called first.
One advantage of doing things this way is that it becomes easier to create wrappers. My custom 'table' theme function could, for example, work like a wrapper around the original one. And by employing jQuery, I can actually accomplish quite a bit without having to re-implement the core logic.

#1
Can you provide a patch for that? This topic overstepped the limits of my JS knowledge and would love to learn more from your example.
#2
mbutcher, any updates on this front?
#3
Here's Drupal.theme from drupal.js:
<?php
Drupal.theme = function(func) {
for (var i = 1, args = []; i < arguments.length; i++) {
args.push(arguments[i]);
}
return (Drupal.theme[func] || Drupal.theme.prototype[func]).apply(this, args);
};
?>
It looks for the theme function in the Drupal.theme namespace, and then in the Drupal.theme.prototype namespace. So, if you define this module's functions in the Drupal.theme.prototype namespace, people using the module can override any of the theme functions this module defines by re-defining it in the Drupal.theme namespace.
To make this possible you would need to replace all occurrences of "Drupal.theme." with "Drupal.theme.prototype." (I count 1 in js_theming.itemlist.js and 4 in js.theming.table.js).
#4
Please submit a patch. thanks. http://drupal.org/patch
#5