Created
June 18, 2012 17:42
-
-
Save francescoagati/2949635 to your computer and use it in GitHub Desktop.
Opal: wrap jquery native methods at runtime with metaprogramming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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