|
# Overview |
|
# ------------------------------ |
|
|
|
# define module |
|
module 'SpaceShipControl.Stupid', (M) -> |
|
|
|
privateMethod = -> '^___^' |
|
|
|
# Inheritance |
|
M.extend 'another.module' |
|
|
|
# set some settings |
|
M.settings '$plugin', true # create JQuery plugin |
|
M.settings |
|
'auto-init': '@stupid' # initialize module on all @stupid elements when page loaded |
|
|
|
# define dom elements that module use |
|
M.elements '@foo', '@bar' |
|
|
|
# define module methods |
|
M.method 'hello', (e) -> |
|
@foo.html 'hello ' + @sum(1, 2) + privateMethod() |
|
@fire 'hello-said' # fire event on module instance |
|
M.method 'sum', (a, b) -> |
|
a + b |
|
|
|
# bind to DOM event |
|
M.event 'click', '@bar', 'hello' |
|
|
|
# DOM |
|
# ------------------------------ |
|
|
|
module 'treeExample', (M) -> |
|
M.tree """ |
|
@root |
|
@foo |
|
@bar |
|
@baz |
|
""" |
|
# ... |
|
|
|
module 'treeExample', (M) -> |
|
M.tree """ |
|
%root |
|
@foo |
|
@baz |
|
""" |
|
# ... |
|
|
|
Foo = module (m) -> |
|
m.elements |
|
submit: '[type=submit]' |
|
agree: '[name=agree]' |
|
m.elements ['.delete a', 'buttom.close'] |
|
m.element 'img@photo' |
|
m.method 'foo', -> |
|
@submit |
|
@agree |
|
@deleteA |
|
@buttonClose |
|
@imgPhoto |
|
|
|
# ... |
|
M.tree """ |
|
ul.buttons |
|
li .button /button /dynamic |
|
""" |
|
M.events """ |
|
click button addButton |
|
""" |
|
M.methods |
|
addButton: -> |
|
@root.append '<li><span class="button">add ' + @count() + '</span></li>' |
|
count: -> |
|
@button().length |
|
|
|
|
|
|
|
# Initialisation |
|
# ------------------------------ |
|
|
|
M.init 'load' # $ -> ..., $(document).on 'html-inserted', -> ... |
|
M.init 'lazy' # listen for module events on document, create instances on any event fired |
|
M.init 'none' # (default) |
|
|
|
M.init 'create' # ??? create module elemnts on DOM |
|
|
|
# Events |
|
# ------------------------------ |
|
|
|
# think about selector dublication |
|
|
|
# DOM events |
|
M.event 'click buttonClose', 'close' |
|
M.events |
|
"click foo": 'bar' |
|
"change foo": -> # ... |
|
"click .button", 'bar' # automatic selector or attr identification? |
|
# `this` in handler — module instance or DOM element? |
|
|
|
# Module events |
|
moduleInstance.on 'abc', (event_parametrs) -> # here this == moduleInstance |
|
moduleInstance.off 'abc' [, -> # ...] |
|
moduleInstance.fire 'abc' [, event_parametrs...] |
|
|
|
M.modulesEvents """ |
|
event-name anotherModule myHandler |
|
""" |
|
|
|
|
|
# Modules interaction |
|
# ------------------------------ |
|
|
|
module 'A', (M) -> |
|
M.tree """ |
|
@A |
|
@button |
|
""" |
|
M.events """ |
|
click button fireFoo |
|
""" |
|
M.methods |
|
hide: -> @root.hide() |
|
fireFoo: -> @fire 'foo' |
|
M.init 'load' |
|
|
|
module 'B', (M) -> |
|
M.modulesEvents """ |
|
foo A hideA |
|
""" |
|
M.methods |
|
hideA: (a) -> a.hide() |
|
M.init 'lazy' |
|
|
|
# VS |
|
|
|
class A |
|
constructor: (@root) -> |
|
@root.on 'click', '@button', => |
|
@root.trigger 'foo' |
|
hide: -> @root.hide() |
|
$ -> |
|
for el in $ '@A' |
|
$(el).data 'A', new A $(el) |
|
|
|
$(document).on 'foo', '@A', -> |
|
$(this).data('A').hide() |
|
|
|
|
|
# Lambda module |
|
# ------------------------------ |
|
|
|
foo = module (M) -> |
|
M.init 'lazy' |
|
M.root '@spoiler' |
|
M.click '@button', -> @find('@content').toggle() |
|
|
|
|
|
# Global events |
|
# ------------------------------ |
|
|
|
module (x) -> |
|
x.root '@scroll' |
|
x.init 'lazy' |
|
x.globalEvent 'window', 'scroll', -> |
|
@root.html $(document).scrollTop() |
|
|
|
module (x) -> |
|
# ... |
|
x.initializer -> |
|
# Can't lazy init in this case |
|
$(window).scroll # ... |
|
|
|
|
|
# Work with 3rd party custom event generator |
|
# ------------------------------ |
|
|
|
module (m) -> |
|
m.root '@scroll' |
|
m.init 'load' |
|
m.initializer -> |
|
ScrollUtil.scroll (scrollTop) => |
|
@root.html scrollTop |
|
|
|
# VS |
|
|
|
$ -> |
|
root = $ '@scroll' |
|
ScrollUtil.scroll (scrollTop) -> |
|
root.html scrollTop |
|
|
|
|
|
|
|
# Page logic |
|
# ------------------------------ |
|
|
|
page "controller#action", -> |
|
a = module (m) -> |
|
# ... |
|
|
|
page "posts#index posts#show", -> |
|
# ... |
|
|