I have a tool palette which shows up when an admin user is logged in to a site. The palette is draggable (via jQueryUI.draggable) and I would like it to remember the position between pages/refreshes... Is there a standard way of doing this, or a plug-in I should be using, or do I need to roll my own (via cookies or something)?
views:
1259answers:
4I'm not aware of plugins or standards for doing this (although it is likely they exist), but its not a terribly complicated operation anyhow. Cookies is one way of accomplishing it.
My preferred method of storing this type of client-side state is to set a callback function which delivers the new state to the server via Ajax. Then, I can do as I please with it. Generally, I'll store it in the session, but there are times when I have decided certain things should persist permanently and moved it from the session to the database. Having this stored server-side lets you make that change easily. I recommend starting by storing it in the session via Ajax callback, and retrieving it on pageload.
I think cookies are a good solution. There is even a jquery cookie plugin here. You could save the coordinates in a cookie on the stop event. When the page changes you could check if the cookie exists and if so change the css on the dragable.
You could save it on the server in a database but that seems a bit overkill to me. Because you will have to change your schema.
Kevin suggested to use the unload event to save the coordinates. This way you don't have to write to the cookie every time the dragging stops.
Cookies are a fine option - you can use the functions by PPK like this:
$('#palette')
.css({ top: readCookie("palletteY"), left: readCookie("paletteX") })
.draggable({ stop: function (event, ui) {
createCookie("paletteX", ui.position.left, 100);
createCookie("paletteY", ui.position.top, 100);
} });