Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Speed of link_results_loop question

Quote Reply
Speed of link_results_loop question
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):
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 Crazy, 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 &amp; Web Development.
rgbworld.com
Quote Reply
Re: [rgbworld] Speed of link_results_loop question In reply to
Quote:
The bad thing about writting the javascript in the plugin is that it writes
the javascript in the plugin Crazy, and API changes could break the plugin.

As far I can see you use Google Maps API, so in this case you can choose which API version to use in your Plugin.

I hope this helps Wink

Cheers,
Boris

Facebook, Twitter and Google+ Auth for GLinks and GCommunity | reCAPTCHA for GLinks | Free GLinks Plugins
Quote Reply
Re: [eupos] Speed of link_results_loop question In reply to
I know I can choose which Google Map API to use ( I use v2).

My question is whether I am better to generate the javascript (map code) IN my plugin, and return a tag to place on the template, or if I am better off looping thru the links in the template itself. Please relook at my orig post to see that there are 2 ways of accompishing the same thing.

I am wondering which of the 2 methods are better, and if there is a speed hit when looping thru the link_loop twice?

Thanks again,
Chris
RGB World, Inc. - Software &amp; Web Development.
rgbworld.com
Quote Reply
Re: [rgbworld] Speed of link_results_loop question In reply to
looping through the results (assuming they array is relatively small [perhaps <1000]), would probably be faster than the function call.

Adrian
Quote Reply
Re: [brewt] Speed of link_results_loop question In reply to
Sorry, I don't understand which way you mean as they both use a function. Unsure

I'll clarify a little.
The plugin searches for links and returns them in a loop like search does.
To display the results I cycle thru the link_results_loop in a results template. Pretty normal.

So the plugin ALWAYS loops thru the results internally. It is very easy to just assemble a
javascript string and return it as a <%markers%> tag that can be dumped on the template
inside a function wrapper. This method 'embeds' the javascript in the plugin and it can't be
edited very easily. You either put the tag in the template or not.

OR

I can cycle thru the link_results_loop twice in the results template.
Once in the javascript function and once for links. This method keeps all javascript
in the template and can be easily edited by user.

Result sets are small: 10-100 per page.

Thank you,
Chris
RGB World, Inc. - Software &amp; Web Development.
rgbworld.com
Quote Reply
Re: [rgbworld] Speed of link_results_loop question In reply to
Oh, I see what you mean. Either way is fine. I doubt there'll be a significant speed difference between the two. However, doing it in the templates is probably a little cleaner (ie. easier to modify later).

Adrian