kmchugh/DavidJS — pull request #3 by kmchugh (2012-02-05 12:33:10+00:00)
Title: Updates to inline module creation.

This update adds the ability to send additional configuration information to a module when it is loaded inline. A module is loaded inline by adding the data-module attribute to an element.

The following attributes are available for inline loading:
- data-module (required) : specifies the name of the module to load
- data-init (optional defaults to 'init' ): specifies the name of the initialisation function on the module
- data-config (optional defaults to {}): specifies a function which should return configuration parameters that will be passed to the initialisation function.

An example of an inline load would look as follows:
- Just the module specified
<div data-module="myLibrary/mymoduleName"/>

- The configuration function specified
<script>function createConfigFunction(toElement){return {myConfigOption : "myValue"};}</script>
<div data-module="myLibrary/mymoduleName" data-config="createConfigFunction"/>

The configuration function will receive the element that the module is loading for as it's parameter.

- The initialisation function specified:
<div data-module="myLibrary/mymoduleName" data-init="alternateInit"/>

This will cause alternateInit to be called where init would normally have been called.

- And finally all specified
<script>function createConfigFunction(toElement){return {myConfigOption : "myValue"};}</script>
<div data-module="myLibrary/mymoduleName" data-config="createConfigFunction" data-init="alternateInit" />

When the initialisation function is called, it will be called with the following parameters:
- toElement : the element that is being configured with the module
- toConfig : the configuration object
- tnIndex : the index of the element in the list of similar elements that are being created

An example of a module would be:

define(["jquery"], function($jQ){

// Make sure the namespace exists
david.namespace("myLibraryNamespace");

myLibraryNamespace.myModuleName = {
init : function(toElement, toConfig, tnIndex){

// Do some initialisation things
}
};

return myLibraryNamespace.myModuleName;
});