3

I can program with a number of client side technologies such as extjs, gxt and others but I am new to jQuery.

I am used to be able to store complex models on the client side and keep referring to it wo going back to the sever.

For example, after login, I want to be able to retain all user info, name, role, etc. Then later, upon clicking a button, the event handler can do different things depending on whether the user is admin or standard user.

How would you implement that in jQuery.

3 Answers 3

1

It's important to note that the problem you are trying to solve was not what jQuery was meant to do or what it was designed for. jQuery is primarily about DOM manipulation (although it has extended some). However, there are a number of other libraries that work in conjunction with jQuery to do what you want to do - primarily KnockoutJS. From Knockout's website: "Easily associate DOM elements with model data using a concise, readable syntax"

In addition, if you just want to store data on the client side, I would recommend looking into localStorage and sessionStorage. Both of these aspects of web programming were designed for the specific problem you are facing. However, be careful with what data you store as this can become a point of data access.

1

for that you need to check on server side by ajax etc. on click button make ajax request and return the response according to the user .if you do only client side than it can be easily change

you can also use the session/cookie to place information about the user and only show according to user for that you can set session like

if($_SESSION['type'] =='admin'){
//show the option which is for admin 

}

if($_SESSION['type'] =='user'){
//show the option which is for admin 

}

above approach will be better and easier approach

like here in so they does not show the delete option for the user who have less than 10k reputation

1

You will find the answer for how to set global variables with both jQuery and JavaScript here:

How to declare a global variable in JavaScript?

jQuery - define a global "namespace" as follows:

$.miniMenu = new Object();

JavaScript - define a global variable:

window.globalVar = "This is global!";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.