ajax add to/delete from favorites

bobdalob - July 8, 2007 - 10:04
Project:Favorite Nodes
Version:5.x-1.x-dev
Component:User interface
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active
Description

might be a nice idea to make the add to + delete from favorites with ajax functionality - good for the user and less reloads?

#1

Dabitch - September 15, 2007 - 09:06

Wish I knew more about Ajax because yes, that sounds awesome.

#2

mr.andrey - October 5, 2007 - 23:56

Yes, this would be great to get going with jquery - so it works like the faves in flickr.

A.

#3

z.stolar - October 8, 2007 - 09:53

I need to implement exactly that in a project. Let's hope a patch will evolve out of that.

#4

bobdalob - December 5, 2007 - 17:20

#2 only just tried the flickr 'faves' - yes, that's the idea!

#5

ilmaestro - December 14, 2007 - 23:38

i'm actually checking how i can implement this same thing. i can get something working using jquery's ajax, but it doesn't actually use the favorite_nodes/add/123 menu path that is built into the favorite nodes module. In general, I don't know if drupal supports ajax calls to URLs this way.

Still researching...

#6

ilmaestro - December 15, 2007 - 01:34

Ok guys, I got it working and thought I'd post my results here. I found inspiration with the fasttoggle module, and just went from there:

The following stuff you can add to the favorite_nodes_menu function. Be sure to empty your cache_menu table after making this change.

function favorite_nodes_menu($may_cache) {
$items = array();

if ($may_cache) {
$items[] = array(
'path'     => 'favoriteNodes/add',
'callback' => 'favorite_nodes_favorites',
'type'     => MENU_CALLBACK,
'access'   => user_access(FAVORITE_NODES_PERM_ADD),
);

$items[] = array(
'path'     => 'favoriteNodes/delete',
'callback' => 'favorite_nodes_favorites',
'type'     => MENU_CALLBACK,
'access'   => user_access(FAVORITE_NODES_PERM_ADD),
);
}
}

Next is the new funciton that is called by the two new menu URLs above:

// Wraps the add favorite nodes function
function favorite_nodes_favorites() {
$op = arg(1);
$nid = arg(2);

if (($op != "add" && $op != "delete") || !is_numeric($nid)) {
return;
}

// The action is confirmed: either via form submit or via AJAX/POST
if (isset($_POST['confirm']) && $_POST['confirm']) {
if ($op == "add") {
$newTitle = "Remove from favorites";
$newHref = "favoriteNodes/delete/$nid";
favorite_nodes_add($nid);
} else {
$newTitle = "Add to favorites";
$newHref = "favoriteNodes/add/$nid";
favorite_nodes_delete($nid);
}

// Output the new status for the updated link text on AJAX changes
if (isset($_POST['javascript']) && $_POST['javascript']) {
drupal_set_header('Content-Type: text/javascript; charset=utf-8');
echo drupal_to_js(array(
"text" => $newTitle,
"callback" => "favorites",
"href" => $newHref)
);
exit;
}
else {
drupal_goto('node/'. $nid);
}
}
}

Finally we have some javascript stuff that kicks off the ajax request to our new menu url handler, and handles the callback:

Drupal.toggle = {
"favorites": function(data) {
$(this).html(data.text);
$(this).attr("href", data.href);
}
};

if (Drupal.jsEnabled) {
$(function() {
$("#toggleFavorites").click(function() {
var link = $(this).addClass('throbbing');

jQuery.post(this.href, { confirm: true, javascript: true }, function(data) {
data = Drupal.parseJson(data);
if (data) {
$("#toggleFavorites").removeClass('throbbing');

// Execute callback
if (data.callback && Drupal.toggle[data.callback]) {
Drupal.toggle[data.callback].call(link[0], data);
}
}
});

// Skip href
return false;
});
});
}

To use the new functionality, just add a link to your page to add to favorites, something like this:

<a nid="106" id="toggleFavorites" href="favoriteNodes/add/106" class="">Add to favorites</a>

Note, the javascript also adds a "throbbing" css class to your link. You can take this stuff out if you want, or download the fasttoggle module to get the CSS and the image for that.

Cheers.

#7

denizengt - January 4, 2008 - 05:39

Tried that code ilmaestro, but it doesn't work? At least not for deleting

#8

MagicMatze - January 4, 2008 - 17:51

same to me - not working. Maybe we made some mistakes by editing your code?

Maybe you could implement this feature in next release?

#9

ilmaestro - January 16, 2008 - 22:28

You say it didn't work for deleting - did it work for adding? The URL for deleting a node from the favorites would of course be different. Example, to delete nid 106:

<a id="toggleFavorites" href="favoriteNodes/delete/106" class="">Delete from favorites</a>

#10

bobdalob - April 16, 2008 - 22:32

Solved.....elsewhere. There are so many modules now that I don't often go and see what's out there. Yesterday I had a browse and found "Views Bookmarks", which does the job. I wasn't sure when I saw the name, but it's worth a look.

I'll leave this request as active for those wishing to see this functionality appear in this module.

#11

lainel - May 10, 2008 - 07:10
Title:ajax add to/delete from favorites» Where...

Where did you insert these codes?
I'm thinking you inserted them into the fasttoggle file.

#12

kbahey - May 10, 2008 - 16:24
Title:Where...» ajax add to/delete from favorites

Please do not change issue titles without a real need for that ....

 
 

Drupal is a registered trademark of Dries Buytaert.