1 of 2
1
Using Favorites with JS libraries and AJAX
Posted: 22 June 2007 11:42 AM   [ Ignore ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

Duh. Somehow half of my post does not show. I will try again.

Profile
 
 
Posted: 22 June 2007 11:48 AM   [ Ignore ]   [ # 1 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

Hello (part two),

while there are many JavaScript libraries out there, I thought I’d post how to use the Favorites module with jQuery. Perhaps others can contribute and add their library of choice to the list and how they accomplished it. I’d be very interested in knowing how others apply AJAX to the favorites module. smile

I’m by no means a JavaScript junkie, so if there is a simpler way of applying jQuery with the Favorites module, do post. My pre-condition was that I wanted to use a proper DOM method and allow for the favorites Module to work with or without Javascript enabled, meaning, leaving all the Javascript goodness out of the HTML.

Profile
 
 
Posted: 22 June 2007 11:49 AM   [ Ignore ]   [ # 2 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

Here are the requirements (what you need):

1. jQuery
2. jqModal plugin

Step one:

Create a template where you store your success/failure message. I named it favorite-add and placed it in my forms template group.
In your favorite-add template, add this code:

{exp:favorites:save}

Profile
 
 
Posted: 22 June 2007 11:49 AM   [ Ignore ]   [ # 3 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

Step two:

Place the favorites link anywhere you want it to appear like so:

<li><a href="{path='forms/favorite-add/'}{entry_id}">Add to favorites</a></li>

Profile
 
 
Posted: 22 June 2007 11:50 AM   [ Ignore ]   [ # 4 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

Step 3

Upload the jquery.js and jqModal.js and jqModal.css onto your server and link to it in your head (remove the space before script):

<link rel='stylesheet' type='text/css' media='screen' href='yoursite.com/jqModal.css' />
<
script src='http://yoursite.com/jquery.js' type='text/javascript'></script>
< script src='http://yoursite.com/jqModal.js' type='text/javascript'></script>

Change the CSS as you deem suitable.

Profile
 
 
Posted: 22 June 2007 11:54 AM   [ Ignore ]   [ # 5 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

Add the required HTML to your template (where you want the modal window to appear):

Example:

<div class="jqmAlert" id="ex3b">

<
div id="ex3b" class="jqmAlertWindow">
    <
div class="jqmAlertTitle clearfix">
    <
a href="#" class="jqmClose"><em>Close</em></a>
  </
div>
  
  <
div class="jqmAlertContent">
  <
p>Please wait... <img src=/images/loading.gif" alt="loading" /></p>
  </div>
</div>

</div>

Profile
 
 
Posted: 22 June 2007 11:55 AM   [ Ignore ]   [ # 6 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

In order for this to work, you need to modify the link we created earlier from this:

<li><a href="{path='forms/favorite-add/'}{entry_id}">Add to favorites</a></li>

to this:

<li><a class="ex3bTrigger" href="{path='forms/favorite-add/'}{entry_id}">Add to favorites</a></li>

Profile
 
 
Posted: 22 June 2007 11:55 AM   [ Ignore ]   [ # 7 ]
Newbie
Rank
Total Posts:  29
Joined  2006-09-16

And the most important part is the function itself. Place this in the template(s) where you want to use it inside the head area (must be below the jquery.js files)

< script type="text/javascript">
$().
ready(function() {
  
var triggers = $('a.ex3bTrigger');
  $(
'#ex3b').jqm({
   trigger
: triggers,
    
ajax: '@href',
    
target: 'div.jqmAlertContent',
    
overlay: 0
    }
);
  if($.
browser.msie) {
  
$('div.jqmAlert .jqmClose').hover(
    function()
{ $(this).addClass('jqmCloseHover'); },
    function()
{ $(this).removeClass('jqmCloseHover'); });
  
}
}
);
</script>

You can see a working sample I created here (click on “save").

That’s it. Completely unobstrusive so that your visitors with js disabled can perform the task as well. Just not as nicely. =)

Hope this helps someone,

Maleika

Profile
 
 
Posted: 22 June 2007 04:37 PM   [ Ignore ]   [ # 8 ]
Newbie
Rank
Total Posts:  12
Joined  2007-06-13

pretty cool maleika. i might just utilize your code here. thanks!

it would be nice to have a login screen, though, right in the pop up that reads “You must be logged in before you can...” on your sample site.

otherwise, perfect.

Profile
 
 
Posted: 23 June 2007 12:40 AM   [ Ignore ]   [ # 9 ]
Sr. Member
Avatar
RankRankRankRank
Total Posts:  336
Joined  2005-07-09

Very nice, thanks for sharing, Maleika.

 Signature 

Ingmar Greil

Profile
 
 
Posted: 28 June 2007 05:38 PM   [ Ignore ]   [ # 10 ]
Newbie
Rank
Total Posts:  12
Joined  2007-04-12

Thank you! Thank you!  I’m sure you saved me hours and hours of work as I just managed to implement AJAX with the Favorites module in under 10 minutes smile

One question, how do I make it so the modal window is initially hidden?

Profile
 
 
Posted: 02 July 2007 01:35 PM   [ Ignore ]   [ # 11 ]
Newbie
Rank
Total Posts:  12
Joined  2007-04-12

Got it - you just have to add “display: none;” to the CSS.

Profile
 
 
Posted: 13 August 2007 04:01 PM   [ Ignore ]   [ # 12 ]
Newbie
Avatar
Rank
Total Posts:  1
Joined  2006-02-24

I took a different approach, having decided not to use a modal window. Thus, you only need jQuery and not the plugin code as well. Here’s the basics of my approach. It needs more sprucing up, such as an indicator of what has changed on the page (yellow background fade perhaps?), but it works.

Here is the basic code for the template that was embedded just above post body -
Don’t forget to pass the entryid along to your embedded template ie - {embed="includes/ajax_inline_favorites" myentryid="{entry_id}"}

{if logged_in}
{exp
:favorites:saved entry_id="{embed:myentryid}"}
<ul class="favoritesul">
{if saved}
<li><em>Saved as a favorite.</em>&nbsp;<a class="Favorites_Trigger" href="{path='forms/favorite-add/'}{embed:myentryid}/delete">Remove from favorites list?</a></li>
{/if}
{if not_saved}
<li><a class="Favorites_Trigger" href="{path='forms/favorite-add/'}{embed:myentryid}">Add this item to your favorites?</a></li>
{/if}
</ul>
{/exp:favorites:saved}
{
/if }

embedded javascript in head of page

</script>

$().ready(function() {
$('a.Favorites_Trigger') .click (function() {
$.get(this.href, function(data) {
$('ul.favoritesul > li')
.
html(data);
});
return
false;
});
});
</script>

The page the add and remove links point to is a very basic template that activates the favorites module:

{exp:favorites:save}

Profile
 
 
Posted: 14 August 2007 10:28 AM   [ Ignore ]   [ # 13 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  701
Joined  2004-03-30

Bill,

I love how elegant jQuery is. My team has been climbing all over it lately. Thank you for sharing your code.

mk

 Signature 

Mitchell Kimbrough

Profile
 
 
Posted: 18 December 2007 10:18 PM   [ Ignore ]   [ # 14 ]
Member
Avatar
RankRankRank
Total Posts:  55
Joined  2006-09-11

Bill, thanks a ton for sharing. Just noting that the particular code Bill shared only works on a unique UL/LI. If you have 3 or 4 LI’s and you click to remove one, the message will show up for all LI’s in the UL. You would need to get the parent item of the one you clicked and replace (or load) the results of the get into that.

Profile
 
 
Posted: 16 January 2008 11:28 PM   [ Ignore ]   [ # 15 ]
Newbie
Rank
Total Posts:  15
Joined  2007-09-15

NOTE: This post is now obsolete. I more carefully read Mitchell’s post above and tried Prototype’s Ajax.Updater. WORKS! Thanks, MK!

mdesign - 18 December 2007 10:18 PM

Bill, thanks a ton for sharing. Just noting that the particular code Bill shared only works on a unique UL/LI. If you have 3 or 4 LI’s and you click to remove one, the message will show up for all LI’s in the UL. You would need to get the parent item of the one you clicked and replace (or load) the results of the get into that.

So… I’ve been beating my brains out trying to do just this.

I can get this stuff to work as advertised within a UL/LI structure, but the duplicate messages appear in all the LI’s. Plus, a UL/LI structure doesn’t work for me design-wise, as I need to put more than one kind of link on the Save to Favorites line.

So I’ve tried to use spans (divs are too problematic side-by-side), and the basic theory I’ve been working on is that by adding an ID to the link tag and a matching class attribute to the surrounding span (or whatever the parent might be), I could identify the parent node without having to use target or srcElement. Each of these identifying attributes contains something like “favespan” plus the {entry_id} for uniqueness.

Getting the class and id injected is easy. Addressing them is giving me gas-- but that’s expected, since my Javascript skills are… well… not really skills. Here’s a bit of what I’m doing:

$().ready(function() {
  
$('a.Favorites_Trigger') .click (function(e) {
   
$.get(this.href, function(data) {
var linkSpan = e.target.id;
$(
'span.'.linkSpan).html(data);
   
});
   return
false;
  
});
});

With the HTML being something like:

<span class="favespan{entry_id}">
<
a class="Favorites_Trigger" id="favespan{entry_id}" href="{path='favorites/add_favorite/'}{entry_id}">Save to Faves</a>
</
span>

The variable linkSpan is being correctly filled with something like “favespan21”, so it looks like the next line of the js is where the problem is. I’m not concatenating properly, or...?

I’m sure it’s something simple. It always is with me.  smile

Thanks!

 Signature 

...Bob

Bob West

––––––––––––––––––––––––––––––––––––––––––––––
Thought Nozzle(TM)
creative consultation:
graphic design . concepts . identity . web . writing
http://www.thoughtnozzle.com/
––––––––––––––––––––––––––––––––––––––––––––––

Profile
 
 
   
1 of 2
1