Hi All,
I am trying to decide the best way to include javascript in templates via a plugin.
It can be done 1 of 2 ways:
1) Write the javascript inside a link_results_loop, like this (method 1):
var index = 0;
var bounds = new GLatLngBounds();
var batch = [];
<%loop link_results_loop~%>
var icon = getDefaultIcon(index);
var point = new GLatLng(<%ZCS_Latitude%>, <%ZCS_Longitude%>);
var marker = createMarker(point, html, icon);
batch.push(marker);
bounds.extend(point);
index++;
<%~endloop%>
var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
map.setCenter(new GLatLng(clat,clng));
map.setZoom(map.getBoundsZoomLevel(bounds));
return batch;
}
or, I can create a tag in the plugin code while finding the results, like this (method 2)
$markers .= "var batch = [];";
while (my $row = $sth->fetchrow_hashref)
{
if($link_count >= $offset and ( $link_count < ($offset + $args->{mh})) ) {
my $link = $links_db->select ('*', { ID => $row->{ID} })->fetchrow_hashref;
$link = Links::SiteHTML::tags('link', $link);
push (@link_results_loop, $link);
$markers .= "var icon = getDefaultIcon(link_count);";
$markers .= "var point = new GLatLng(". $link->{ZCS_Latitude} .",". $link->{ZCS_Longitude} .");";
$markers .= "var marker = createMarker(point, html, icon);";
$markers .= "batch.push(marker);";
$markers .= "bounds.extend(point);";
}
$link_count++;
}
$markers .= "var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;";
$markers .= "var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;";
$markers .= "map.setCenter(new GLatLng(clat,clng));";
$markers .= "map.setZoom(map.getBoundsZoomLevel(bounds));";
Then use method 2 in template like this:
function getMarkers() {
<%markers%>
}
The difference is that if I create the javascript in the plugin (method 2), I do not have to
run through the link loop twice (once for js and once for link loop).
Should I be concerned about running thru the link loop twice? ( I don't HAVE to).
The bad thing about writting the javascript in the plugin is that it writes
the javascript in the plugin
, and API changes could break the plugin.
Also, the javascript is not easily editable if in the plugin.
The benefits of writing js in plugin is that users can't break it, and only 1 link loop.
Thanks for any comments.
Chris
RGB World, Inc. - Software & Web Development.
rgbworld.com
I am trying to decide the best way to include javascript in templates via a plugin.
It can be done 1 of 2 ways:
1) Write the javascript inside a link_results_loop, like this (method 1):
Code:
function getMarkers() { var index = 0;
var bounds = new GLatLngBounds();
var batch = [];
<%loop link_results_loop~%>
var icon = getDefaultIcon(index);
var point = new GLatLng(<%ZCS_Latitude%>, <%ZCS_Longitude%>);
var marker = createMarker(point, html, icon);
batch.push(marker);
bounds.extend(point);
index++;
<%~endloop%>
var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
map.setCenter(new GLatLng(clat,clng));
map.setZoom(map.getBoundsZoomLevel(bounds));
return batch;
}
or, I can create a tag in the plugin code while finding the results, like this (method 2)
Code:
$markers = "new GLatLngBounds();"; $markers .= "var batch = [];";
while (my $row = $sth->fetchrow_hashref)
{
if($link_count >= $offset and ( $link_count < ($offset + $args->{mh})) ) {
my $link = $links_db->select ('*', { ID => $row->{ID} })->fetchrow_hashref;
$link = Links::SiteHTML::tags('link', $link);
push (@link_results_loop, $link);
$markers .= "var icon = getDefaultIcon(link_count);";
$markers .= "var point = new GLatLng(". $link->{ZCS_Latitude} .",". $link->{ZCS_Longitude} .");";
$markers .= "var marker = createMarker(point, html, icon);";
$markers .= "batch.push(marker);";
$markers .= "bounds.extend(point);";
}
$link_count++;
}
$markers .= "var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;";
$markers .= "var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;";
$markers .= "map.setCenter(new GLatLng(clat,clng));";
$markers .= "map.setZoom(map.getBoundsZoomLevel(bounds));";
Then use method 2 in template like this:
function getMarkers() {
<%markers%>
}
The difference is that if I create the javascript in the plugin (method 2), I do not have to
run through the link loop twice (once for js and once for link loop).
Should I be concerned about running thru the link loop twice? ( I don't HAVE to).
The bad thing about writting the javascript in the plugin is that it writes
the javascript in the plugin

Also, the javascript is not easily editable if in the plugin.
The benefits of writing js in plugin is that users can't break it, and only 1 link loop.
Thanks for any comments.
Chris
RGB World, Inc. - Software & Web Development.
rgbworld.com