Namespaces: New themes in modules should use Drupal.theme.prototype

mbutcher - September 22, 2008 - 16:30
Project:JavaScript Theming
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Description

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

litwol - October 22, 2008 - 01:52

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

litwol - December 19, 2008 - 06:05

mbutcher, any updates on this front?

#3

jpmckinney - October 11, 2009 - 21:10
Status:active» needs review

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

litwol - October 11, 2009 - 22:31
Status:needs review» needs work

Please submit a patch. thanks. http://drupal.org/patch

#5

jpmckinney - October 15, 2009 - 16:39
Status:needs work» needs review
AttachmentSize
example.patch 2.29 KB
 
 

Drupal is a registered trademark of Dries Buytaert.