Skip to content

Instantly share code, notes, and snippets.

@mcmoyer
Last active December 11, 2015 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcmoyer/4679081 to your computer and use it in GitHub Desktop.
Save mcmoyer/4679081 to your computer and use it in GitHub Desktop.
my general coffeescript/javascript style
#embed this in your body tag
# %body{body_data}
def body_data
{
:"data-controller" => params[:controller],
:"data-action" => params[:action],
:"data-id" => params[:id],
:"data-parent-controller" => parent_controller,
:"data-parent-id" => params["#{parent_controller.to_s.singularize}_id".to_sym]
}
end
#app/assets/javascripts/initialize.js.coffee
window.APP =
common: ->
console.log "common base function"
jQuery ->
controller = $("body").data("controller")
action = $("body").data("action")
app = window.APP
fn = app['common']
if typeof(fn) == 'function'
fn.call()
if app[controller]
fnc = app[controller]['common']
if typeof(fnc) == 'function'
fnc.call()
fna = app[controller][action]
if typeof(fna) == 'function'
fna.call()
#app/assets/javascripts/page_objects/products.js.coffee
$.extend window.APP,
products:
common: ->
#console.log "common functions"
index: ->
#console.log "index action functions"
show: ->
new GalleryColorBox()
class GalleryColorBox
constructor: ->
$("a.gallery").colorbox(rel: 'variants')
$("div.product-variants").delegate("a.change-picture", "click", (e) ->
e.preventDefault()
base = $(e.target).parents("div.variant-panel")
key = base.data("key")
product = $("body").data("id")
console.log product, key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment