Standardize how JS XModules are instantiated

This commit is contained in:
Calen Pennington
2012-07-20 11:54:58 -04:00
parent 50f27bcfb7
commit 408726e678
13 changed files with 91 additions and 53 deletions

View File

@@ -0,0 +1,30 @@
@XModule =
###
Load a single module (either an edit module or a display module)
from the supplied element, which should have a data-type attribute
specifying the class to load
###
loadModule: (element) ->
moduleType = $(element).data('type')
if moduleType == 'None'
return
try
new window[moduleType](element)
catch error
console.error "Unable to load #{moduleType}: #{error.message}" if console
###
Load all modules on the page of the specified type.
If container is provided, only load modules inside that element
Type is one of 'display' or 'edit'
###
loadModules: (type, container) ->
selector = ".xmodule_#{type}"
if container?
modules = $(container).find(selector)
else
modules = $(selector)
modules.each (idx, element) -> XModule.loadModule element