Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created July 17, 2011 01:57
Show Gist options
  • Save francescoagati/1087021 to your computer and use it in GitHub Desktop.
Save francescoagati/1087021 to your computer and use it in GitHub Desktop.
jquery declarative named scope
$.fn.declare.add
'set.color': (color) -> @css({'color':color})
'set.bgcolor': (color) -> @css({'background-color':color})
$("div").declare('set.color','green').declare('set.bgcolor','red')
#
# an example of named scope like in activerecord
# implemented for jquery
#
#
do ->
scopes_cache={}
fn= (scope,args...) -> scopes_cache[scope].apply(this,args)
fn.add= (scopes) -> scopes_cache[scope]=fn for scope,fn of scopes
fn.remove= (scope) -> delete scopes_cache[scope]
jQuery.fn.declare=fn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment