Permalink
Browse files

r20772@GOP: gugod | 2008-02-09 20:48:03 +0800

 Add a "closure" option to as_html method, to generate a script tag
 that contains a closure, and its context is the script node itself.
 
 Move the document of basic helpers into Writer.pm, and include those
 helpers by default.
 


git-svn-id: http://code.handlino.com/svn/perl5/JavaScript-Writer@202 b19cb30a-9f2e-4084-8847-5e1a13269302
  • Loading branch information...
1 parent 8bd18eb commit 6f5cc714f7dbae75b518cdb7551cac230200f348 @gugod committed Feb 9, 2008
Showing with 99 additions and 63 deletions.
  1. +83 −4 lib/JavaScript/Writer.pm
  2. +2 −58 lib/JavaScript/Writer/BasicHelpers.pm
  3. +14 −1 t/11.closure.t
View
@@ -250,17 +250,37 @@ sub as_string {
return $ret;
}
-sub as_html {
- qq{<script type="text/javascript">${\self->as_string}</script>}
+require JavaScript::Writer::BasicHelpers;
+
+{
+ my $sn = 0;
+ sub as_html {
+ my ($self, %param) = @_;
+ $sn++;
+
+ my $id = "javascript-writer-$$-$sn";
+
+ if ($param{closure}) {
+ my $j = JavaScript::Writer->new;
+ my $self_code = $self->as_string;
+ $j->closure(
+ this => \ "document.getElementById('$id')",
+ body => sub {
+ js->append( $self_code );
+ }
+ );
+ return qq{<script id="$id" type="text/javascript">$j</script>}
+ }
+ return qq{<script type="text/javascript">$self</script>};
+ }
}
our $AUTOLOAD;
sub AUTOLOAD {
- my $self = shift;
my $function = $AUTOLOAD;
$function =~ s/.*:://;
- return $self->call($function, @_);
+ return self->call($function, args);
}
1; # Magic true value required at end of module
@@ -467,6 +487,65 @@ stringify to this string:
function(){alert("Nihao")}
+=head2 closure(&block)
+
+Generate a closure with body &block. This means to generate a
+construct like this:
+
+ ;(function(){
+ // ...
+ })();
+
+It's very useful for doing functional programming in javascript.
+
+
+=head2 closure(arguments => { name => value }, body => sub {... }, ...)
+
+Another form of the closure function. For example:
+
+ js->closure(
+ parameters => {
+ el => "document.getElementById('foo')",
+ var1 => "var1",
+ var2 => \ "var 2 value"
+ },
+ body => sub {
+ ...
+ }
+ );
+
+This generates something like this:
+
+ ;(function(el, var1, var2){
+ ...
+ })(document.getElementById('foo'), var1, "var 2 value");
+
+The value to the key "parameters" is a hashref, which means the order
+of function arguments is not guarenteed. But that shouldn't matter at
+all because they are all named. They have to be named anyway.
+
+The value to the key "this" refers to te value of "this" variable in
+the closure. For example:
+
+ js->closure(
+ this => "el",
+ parameters => { msg => \ "Hello, World" }
+ body => sub {
+ js->jQuery("this")->html("msg");
+ }
+ );
+
+This generates
+
+ ;(function(msg){
+ jQuery(this).html(msg);
+ }).call(el, "Hello, World");
+
+=head2 delay($n, &block)
+
+Generate a piece of code that delays the execution of &block for $n
+seconds.
+
=item append( $statement )
Manually append a statement. With this function, you need to properly
@@ -45,6 +45,7 @@ sub closure {
return $self;
}
+
1;
__END__
@@ -61,64 +62,7 @@ painful.
=head1 METHODS
-=head2 delay($n, &block)
-
-Generate a piece of code that delays the execution of &block for $n
-seconds.
-
-=head2 closure(&block)
-
-Generate a closure with body &block. This means to generate a
-construct like this:
-
- ;(function(){
- // ...
- })();
-
-It's very useful for doing functional programming in javascript.
-
-
-=head2 closure(arguments => { name => value }, body => sub {... }, ...)
-
-Another form of the closure function. For example:
-
- js->closure(
- parameters => {
- el => "document.getElementById('foo')",
- var1 => "var1",
- var2 => \ "var 2 value"
- },
- body => sub {
- ...
- }
- );
-
-This generates something like this:
-
- ;(function(el, var1, var2){
- ...
- })(document.getElementById('foo'), var1, "var 2 value");
-
-The value to the key "parameters" is a hashref, which means the order
-of function arguments is not guarenteed. But that shouldn't matter at
-all because they are all named. They have to be named anyway.
-
-The value to the key "this" refers to te value of "this" variable in
-the closure. For example:
-
- js->closure(
- this => "el",
- parameters => { msg => \ "Hello, World" }
- body => sub {
- js->jQuery("this")->html("msg");
- }
- );
-
-This generates
-
- ;(function(msg){
- jQuery(this).html(msg);
- }).call(el, "Hello, World");
+Method documentations are put into L<JavaScript::Writer>.
=head1 AUTHOR and LICENSE
View
@@ -5,7 +5,19 @@ use JavaScript::Writer;
use JavaScript::Writer::BasicHelpers;
use Test::More;
-plan tests => 3;
+plan tests => 4;
+
+
+{
+ js->new;
+ js->alert(42);
+ js->setTimeout(sub { js->alert("Hi"); }, 42);
+
+ my $code = js->as_string;
+ my $html = js->as_html(closure => 1);
+
+ like($html, qr{<script id="(javascript-writer-\d+-\d+)" type="text/javascript">;\(function\(\){\Q$code\E}\).call\(document.getElementById\('\1'\)\);</script>}s);
+}
{
my $js = JavaScript::Writer->new;
@@ -56,3 +68,4 @@ plan tests => 3;
);
}
+

0 comments on commit 6f5cc71

Please sign in to comment.