Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created June 18, 2012 17:42
Show Gist options
  • Save francescoagati/2949635 to your computer and use it in GitHub Desktop.
Save francescoagati/2949635 to your computer and use it in GitHub Desktop.
Opal: wrap jquery native methods at runtime with metaprogramming
module Jquery
module WrapMethods
def WrapMethods.included(base)
# i can't use extend on opal. why?
class << base
def wrap_method(meth)
define_method(meth) do |*args|
`
var sel=jQuery(this.selector);
sel[meth].apply(sel,args);
`
self
end
end
def wrap_methods(*methods)
methods.each { |method| wrap_method(method) }
end
end
end
end
class Base
include WrapMethods
def initialize(selector)
@selector=selector
end
wrap_methods :html,:before,:after,:wrap
end
def Jquery.sel(selector)
Base.new(selector)
end
end
Document.ready? do
Jquery.sel("h1").html("ciao").before("<p>ciao</p>").after("<p>ciao2</p>")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment