Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Multiple function calls from links


Reply

3 Posts   691 Views

Avatar
LisaB

26 June 2012 at 2:59am Community Member, 28 Posts

Hi all, In my template, ServiceProductHolder.ss, I have a control loop listing multiple ServiceProducts and within the control block I have:

<p><a href="$saveToMember">Add to My Programme</a></p>

I want saveToMember to run when the link is clicked, but it is obviously running for every ServiceProduct listed when the page loads. This must be a problem with my template syntax - how can I stop this and run it only when the link is clicked?

I have something similar working elsewhere using something like:

<a href="{$Top.Link}functionName">Do function</a>

but because this function is on the page controller it's slightly different.

Any help much appreciated!

Avatar
LisaB

27 June 2012 at 1:36am Community Member, 28 Posts

Another thought on this. It seems it might not be possible to call my function that is on the data object from a link click because you can't call it via a URL as you could if it were on the page controller. If you put it in the template like I have above it will always run on page load when the template is parsed.

So, I started to look at doing this with a form and now I have a new problem with where to put the code. Because the easiest way to create the relation is in the ServiceProduct data object like this:

$member->ServiceProducts()->add($this);

And because the form needs to be displayed within the control block, I put the form in ServiceProduct.php. However I get method link() doesn't exist on ServiceProduct when trying to visit the holder page.

However, if I put the form in the page controller, I'm not sure how I'd get the current ServiceProduct ID out of the loop to relate it to the member. I'm sure I'm missing something easy here but it's been ages since I looked at any of this stuff.

I'm thinking of going back to a text link and ajax but will have to learn about it first!

Avatar
LisaB

5 July 2012 at 1:58am Community Member, 28 Posts

For posterity, in case it's ever useful to anyone:

"However, if I put the form in the page controller, I'm not sure how I'd get the current ServiceProduct ID out of the loop to relate it to the member. I'm sure I'm missing something easy here but it's been ages since I looked at any of this stuff."

I was missing something easy. I put everything in the page controller rather than the data object, then from within my loop on the page template where I was controlling the data objects I used <a href="{$Top.Link}functionname/$ID">link</a>. The function gets that DO from the url parameter in order to create or destroy the relationship between it and the logged in member.

Thanks to r3v3rb and other on IRC for pointing me in the right direction.