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 |
Jump to:
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
Wish I knew more about Ajax because yes, that sounds awesome.
#2
Yes, this would be great to get going with jquery - so it works like the faves in flickr.
A.
#3
I need to implement exactly that in a project. Let's hope a patch will evolve out of that.
#4
#2 only just tried the flickr 'faves' - yes, that's the idea!
#5
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
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
Tried that code ilmaestro, but it doesn't work? At least not for deleting
#8
same to me - not working. Maybe we made some mistakes by editing your code?
Maybe you could implement this feature in next release?
#9
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
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
Where did you insert these codes?
I'm thinking you inserted them into the fasttoggle file.
#12
Please do not change issue titles without a real need for that ....