NYCPHP Meetup

NYPHP.org

[nycphp-talk] AJAX and State

bz-gmort at beezifies.com bz-gmort at beezifies.com
Wed Sep 5 08:20:56 EDT 2007


Hans Zaunere wrote:
> Ajax solves this problem neatly by letting you move all state [1] into
>> the browser.  This makes sense from an architectural viewpoint because
>> we are putting this context information close to where it is needed,
>> the UI.
>>     
>
> I'm not sure what you mean, though, by keeping state in the browser.  Here's
> essentially what's work for me in the past:P
>   


I read the above as, to take a simple example:

A user searches for all employee records in NYC. 
He browses to page 3 of that result set
Than he clicks on a record to edit it.
When he completes editing the record and clicks "save", he wants to be 
returned to page 3 of the result set, with a message across the top of 
the screen saying "Record XYZ updated successfully".

With sessions, you could store the search and page number he was on in 
the session, so when he clicks save he returns to the page he came from.

Problem:
A user searches for all employee records in NYC. 
He browses to page 3 of that result set
Than he clicks on a record to edit it.
He doesn't recall the exact phrase used that he wants to change some 
piece of data to to adopt a standard.
He opens a second tab/browser and searches for all employees in Baltimore.
He browses through to the fourth page and finds a record he used that on 
and opens it
He then switches back to the window where he is editing the record and 
copies the phrase from one window to the other.
He completes processing and clicks save.
He is returned to page 4 of the Baltimore search(his last viewed search 
page) and not the NY search


Ajax tracking state:
A user searches for all employee records in NYC
The search result is loaded in a hidden div which is then made visible 
via AJAX
He browses to page 3 of the search results
Then he clicks on a record to edit it
Using Ajax, the listing is now hidden and the record is loaded in a 
hidden div and then made visible
He doesn't recall the exact phrase...
....
(user now has 2 windows open, 1 with the NYC employee being edited, 1 
with the Baltimore employee being edited - yes this is stupid, he should 
be viewing not editing the Baltimore data, but such is the way of life)
He makes the change to the NYC employee and clicks save
using AJAX, the data is saved, the employee edit div data is set to 
"employee XYZ successfully update", while the employe list div is made 
visible.

So in this manner, using Ajax his path/state is saved in the browser 
window and he returns to precisely where he was.


The above being a stupid example that could have been easily tracked in 
other ways(a single line of data in each row specifying the search 
criteria, the page, and perhaps the result set data identifier).  But 
when you have 30 or 40 pieces of data to track and return to the user, 
it starts getting more complicated.

Of course, all this could have been done with sessions and setting an 
identifier for each browser window.  So I would not say this is 
something "extra" you get with Ajax.  What it rather does is make less 
work for the coder.  Using cookies, sessions, or passing variables 
through the forms you would have had to actively code for all that state 
information.  By keeping it all stored in the browser and not reloading 
the data, you don't have to track it at all.


OTOH it does have it's own price and problems.  All these nifty little 
ajax scripts maintaining data on a browser start to eat up memory.  
Browsers that weren't designed on the assumption of maintaining and 
adding more and more variables, instead they assumed each page 
navigation would discard all the old variables and create a new set will 
start leaking memory.  And after sitting on one of these cool sites for 
4-6 hours, your browser could start dragging the browser or system 
down.  In addition, since all these links ARE often links, you get 
someone like me who comes along and opens up windows in new tabs when he 
wants to work on something specific, and you can get a lot of 
unpredictable results if you were counting on the data in the browser to 
be there for context and I suddenly opened a new tab.

But hey, everything has a cost, right?



More information about the talk mailing list