views:

315

answers:

2

My application (Asp.Net MVC) has great interaction with the user interface (jQuery/js). For example, setting various searches charts, moving the gadgets on the screen and more .. I of course want to keep all data for each user. So that data will be available from any page in the Dumaine and the user will accepts his preferences.

Now I keep all data in a cookie because it did not seem logical asynchronous access to the server each time the user changes something and thet happens a lot.When the user logout from the application I save the cookie to the database.

The Q is how to save the settings back to the db - from the client to the server. because the are a lot of interactin that I want to record. example scanrios: closing widget,moving widget,resizing menues, ordering columens.. I want to record that actions. if I will fire ajax saving rutine for each action ןt will be too cumbersome. Maybe I have no choice.. Maybe I should run an asynchronous saving all of a certain interval seconds.

The problem is the cookie becomes very large. The thought that this huge cookie is attached to each server request makes me feel that my attitude is wrong.

Another problem cookies have size limit. It varies from your browser but I definitely have been close to the border - my cookie easily become 4kb

Is there another solution?

A: 

Another solution would be to store the user preferences into the session and write some server side logic (like action filter) that would write those preferences as JSON encoded string on each page (in a script tag towards the end of the markup) making them available to client scripts.

Darin Dimitrov
Thanks the answer,please see the edit above.
ari
+2  A: 

Without knowing your code, have you considered storing the users preferences in a/your database. A UserPreference table with columns for various settings is a possibility.

You could update it via AJAX/JSON if you had a 'Save Preferences' option, or just update it on postback.

EDIT 1: After thinking about it, I think having an explicit 'save preferences' button would be beneficial and practical.

Somewhere on your page, where the use edits the things that generate the cookie, put an button called save, then hook up a jQuery click handler. On click, build a CSV string or another method of storing the preferences for posting back to the server, then use $.post to send it back to an action method in a controller.

Once there, store it in the database somehow (up to you exactly how), then return a JSON array with a success attribute, to denote whether the preference storing was successful.

When the page is loading, get the preferences out of the database and perform you manipulation.

Alastair Pitts
Thanks the answer,please see the edit above.
ari
As I understand it, you are using Javascript to build the cookie every time a setting is changed?
Alastair Pitts
@Alastair, yes! only cookie..
ari
@ari: I've updated with the way I would do it.
Alastair Pitts
@Alastair,I want to get rid of the cookie (large cookie - performance issue).i think the way to go is with json obj on the client and blob storing on the server. the only issue is with the frequency of the ajax calls. I think that I will solve it in diffrent way. when user change somthing I will raise a dirty flag.then I will check with some interval that dirty flag and when its dirty the ajax call will invoked. with that way user can do diffrent of customiztion stuff. the ajax call begin only with the timer. what do you think?
ari
@ari: Personally I think you should leave the saving of preferences up to the user. This means that they can make changes without the concern of the timed save activating at a poor time (mid change, funny values etc). This will also make it easier for you to debug and manage the frequency of calls.
Alastair Pitts