Skip to content

Commit

Permalink
r20727@GOP: gugod | 2008-02-03 09:49:21 +0800
Browse files Browse the repository at this point in the history
 JQueryHelper is renamed to jQueryHelper.
 
 Implement a jQuery helper function in Perl that can do both:
 
    jQuery("#selector")
 
 and
 
    jQuery.ajax( ... ) 
 
 Add an "obj_as_string" routine that can dump functions as strings.
 


git-svn-id: http://code.handlino.com/svn/perl5/JavaScript-Writer@196 b19cb30a-9f2e-4084-8847-5e1a13269302
  • Loading branch information
gugod committed Feb 8, 2008
1 parent aeaa157 commit 2e60f5d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 49 deletions.
52 changes: 38 additions & 14 deletions lib/JavaScript/Writer.pm
Expand Up @@ -50,9 +50,7 @@ sub js {
if (defined $target) {
$base->{target} = $target;
}

return $base;

}

sub new {
Expand Down Expand Up @@ -198,6 +196,37 @@ sub function {
return JavaScript::Writer::Function->new(@args);
}

sub obj_as_string {
my ($obj) = args;

if (ref($obj) eq 'CODE') {
return self->function($obj)
}
elsif (ref($obj) =~ /^JavaScript::Writer/) {
return $obj->as_string
}
elsif (ref($obj) eq "") {
return $obj
}
elsif (ref($obj) eq "SCALAR") {
return JSON::Syck::Dump($$obj)
}
elsif (ref($obj) eq 'ARRAY') {
my @ret = map {
self->obj_as_string($_)
} @$obj;

return "[" . join(",", @ret) . "]";
}
elsif (ref($obj) eq 'HASH') {
my %ret;
while (my ($k, $v) = each %$obj) {
$ret{$k} = self->obj_as_string($v)
}
return "{" . join (",", map { JSON::Syck::Dump($_) . ":" . $ret{$_} } keys %ret) . "}";
}
}

sub as_string {
my $ret = "";

Expand All @@ -210,18 +239,7 @@ sub as_string {
"$f(" .
join(",",
map {
if (ref($_) eq 'CODE') {
self->function($_)
}
elsif (ref($_) =~ /^JavaScript::Writer/) {
$_->as_string
}
elsif (ref($_) eq "") {
$_
}
else {
JSON::Syck::Dump $_
}
self->obj_as_string( $_ )
} @$args
) . ")" . $delimiter
}
Expand Down Expand Up @@ -455,6 +473,12 @@ doing.
Output your statements as a snippet of javascript code.
=item obj_as_string()
This is use internally as a wrapper to JSON::Syck::Dump sub-routine,
in order to allow functions to be dumped as a part of javascript
object.
=item as_html()
Output your javascript statements as a snippet with a <script> tag.
Expand Down
34 changes: 0 additions & 34 deletions lib/JavaScript/Writer/JQueryHelper.pm

This file was deleted.

44 changes: 44 additions & 0 deletions lib/JavaScript/Writer/jQueryHelper.pm
@@ -0,0 +1,44 @@
use strict;
use warnings;

package JavaScript::Writer::jQueryHelper;

our $VERSION = '0.0.2';

use JavaScript::Writer;

use Sub::Exporter -setup => {
exports => ['jQuery'],
groups => {
default => [ -all ],
}
};

sub jQuery {
my ($selector) = @_;
if (!defined $selector) {
return js->object('jQuery')
}
elsif (ref($selector) eq '') {
return js->call('jQuery', \ $selector);
}
}

1;

__END__
=head1 NAME
JavaScript::Writer::JQueryHelper - Helper methods powered by jQuery.
=head1 SYNOPSIS
=head1 METHODS
=head1 DESCRIPTION
=cut
59 changes: 58 additions & 1 deletion t/100.jquery.t
Expand Up @@ -3,10 +3,11 @@
use strict;
use warnings;
use JavaScript::Writer;
use JavaScript::Writer::jQueryHelper;

use Test::More;

plan tests => 4;
plan tests => 7;

my $wanted = 'jQuery("#foo").bar();';

Expand Down Expand Up @@ -47,3 +48,59 @@ my $wanted = 'jQuery("#foo").bar();';
"It can write a jQuery with callback."
)
}

{
js->new;

jQuery('#foo')->click(
sub {
js->alert(\ "Nihao");
}
);

is(
js->as_string,
q{jQuery("#foo").click(function(){alert("Nihao");});},
"with jQuery( selector ) from jQueryHelper"
)
}

{
js->new;

jQuery->get(
\ "foo.json",
sub {
js->alert(\ "Nihao");
}
);

js->alert(42);

is(
js->as_string,
q{jQuery.get("foo.json",function(){alert("Nihao");});alert(42);},
"with jQuery from jQueryHelper"
)
}

{
js->new;

my $cb = js->function(sub {
js->alert(\ "Nihao");
});

jQuery->ajax({
url => "foo.json",
success => $cb
});

js->alert(42);

is(
js->as_string,
'jQuery.ajax({"success":function(){alert("Nihao");},"url":foo.json});alert(42);',
"with jQuery from jQueryHelper"
)
}

0 comments on commit 2e60f5d

Please sign in to comment.