views:

414

answers:

6

I am trying to use jQuery AJAX. What my requirement is, i wish to load user names from DB in dataset, convert it to JSON format and store it in memory or using jQuery data for use while a user is browsing my site, i.e for a session. This way I can use autocomplete or my own code to display data to user.

Can anyone help me design such a scenario?

A: 

JQuery data may not be useful because you will lose the data when you go from one page to the next as the page is refreshed, you can however do this using COOKIES -- there is a jQuery cookies plugin that will make it easier for you.

DMin
If the list is very long, the user will be sending that cookie to the server every. single. request. That could cause Hell for your request headers buffers on your server. If the list is smaller than 8k, it should work, though.
Zack
A: 

Sessions means serverside. jQuery means clientside. You can't translate between the two. Cookies is the best you can hope for to remain strictly clientside.

drachenstern
By sessions means that I could store my data from db in some memory which could be searchable via jQuery as a datastore. It could be anything like an jscript array or even jQuery.data(). Just that I do not want my data to be fetched everytime from db.
Ashish
You can avoid hitting the DB with memcached or writing the JSON to a file.
Zack
Cookies is second best. Better than cookies on client side, although not as widely supported (i.e. older browsers) is HTML 5 Web Storage: http://www.w3.org/TR/webstorage/
John K
@jdk - if HTML5 were in widespread use, I would say target that. @Ted you cant get to the session if you're on the client. End of story. You have to send it to the client. That means per page, which includes postback to sync the two. At that point, it is neither session nor cookie, just data on a form (form being a generic method of storing data in a browser pane).
drachenstern
A: 

Google Gears is the only solution I know of for doing this, but it requires the user to install it and only runs on a few browsers. You could, however, use $.getJSON to fetch a list of users, JSON-encoded and gzipped, from your server when the user say... focuses the auto-completed search box, then use aggressive client-side resource caching to reduce the number of hits on your database.

If it's a pretty long list of users, you may want to look into using memcached to store the list, or perhaps writing it to a file and having it be served directly by your front-end server (i.e. Nginx, Lighttpd, Apache: pretty much anything that avoids running a DB query)

Zack
+2  A: 

HTML 5 Web Storage

think beyond cookies, Google Gears and proprietary solutions

Standardized, no special plugin required. Scriptable through JavaScript/jQuery. Although supported only in the latest browsers, you can use HTML 5 Web Storage, namely localStorage and sessionStorage properties, intended to keep state on the client side in context of the website between page requests. Holds a heck of a lot more than cookies. For example IE 8 DOM storage spec (10 MB client-side storage), Firefox DOM storage spec.

John K
+1  A: 

There is also the YUI 2 Storage Utility which abstracts the storage for you (HTML 5, Google Gears, SWF) depending on what the browser supports:

The Storage Utility provides a mechanism for storing significant amounts of textual data, client-side, whether or not your browsers supports the proposed HTML 5 Storage specification.

Peter McGrattan
A: 

I've been using PersistJS with great success to do pretty much what you're describing. It's a lightweight plugin (< 10k) that abstracts away client-side storage using any of these available backends:

  • flash: Flash 8 persistent storage.
  • gears: Google Gears-based persistent storage.
  • localstorage: HTML5 draft storage.
  • whatwg_db: HTML5 draft database storage.
  • globalstorage: HTML5 draft storage (old spec).
  • ie: Internet Explorer userdata behaviors.
  • cookie: Cookie-based persistent storage.

It transparently picks the best storage backend based on browser capability and installed plugins. It works with:

  • globalStorage: Firefox 2.0+, Internet Explorer 8
  • localStorage: development WebKit
  • openDatabase: Safari 3.1+
  • userdata behavior: Internet Explorer 5.5+

...and optionally falls back to cookies if none of the other persistent storage options work. The DOM storage methods get you at least a few megabytes worth of storage space; you'll be limited to roughly 4kB if you fall back to cookies.

josh3736