vendor/assets/javascripts/modularity.js.coffee in modularity-rails-0.1.0 vs vendor/assets/javascripts/modularity.js.coffee in modularity-rails-0.2.0
- old
+ new
@@ -4,29 +4,38 @@
#
# Please see https://github.com/kevgo/modularity for more information.
class window.Module
- # The container variable is required. Provide 'null' in tests.
+ # The container variable is required. Provide 'testing' in tests.
constructor: (@container) ->
- alert('Module constructor: No container given.') unless @container? and @container.length > 0
+ return alert 'Error in Module constructor: No container given.' unless @container?
+ if @container != 'testing'
+ return alert 'Error in Module constructor: The given container must be a jQuery object.' unless typeof container.jquery == 'string'
+ return alert "Error in Module constructor: The given container ('#{container.selector}') is empty." unless @container? and @container.length > 0
+ return alert "Error in Module constructor: The given container ('#{container.selector}') has more than one element." unless @container? and @container.length == 1
- # Checks whether the given condition is true.
- # Shows an alert with the given message if not.
+ # Checks whether the given condition is true.
+ # Shows an alert with the given message if not.
assert: (condition, message) ->
- alert(message) unless condition? and condition.length > 0
+ alert(message) unless condition?.length > 0
+ # MODULE EVENTS.
+
# Calls the given function when this widget fires the given local event.
bind_event: (event_type, callback) =>
@assert event_type, "Module.bind_event: parameter 'event_type' is empty"
@container.bind event_type, callback
# Fires the given local event with the given data payload.
fire_event: (event_type, data) =>
@assert event_type, 'Module.fire_event: You must provide the event type to fire.'
@container.trigger event_type, data or {}
+
+ # GLOBAL EVENTS.
+
# Subscribes to the given global event,
# i.e. calls the given function when the given global event type happens.
bind_global_event: (event_type, callback) =>
@assert event_type, "Module.bind_global_event: parameter 'event_type' is empty"
@global_event_container().bind event_type, callback
@@ -37,5 +46,26 @@
@global_event_container().trigger event_type, data or []
# Returns the DOM object that is used to fire global events on.
global_event_container: =>
@global_event_container_cache or= $(window)
+
+
+# jQuery integration for creating Modules.
+#
+# Call like this: myModule = $('...').module(MyModuleClass)
+#
+# Parameters:
+# * klass: the class of the Module to instantiate
+# * any additional parameters are forwarded to the Module constructor.
+# Returns the created module instance.
+#
+# Messages errors in alert boxes.
+#
+jQuery.fn.module = (klass, args...) ->
+
+ # Check parameters.
+ if typeof klass != 'function'
+ return alert "ERROR!\n\nYou must provide the Module class when calling $.module().\n\nExample: $('...').module(MyModuleClass)\n\nYou provided: #{klass} (#{typeof klass})"
+
+ # Instantiate the class and return the instance.
+ new klass(this, args...)