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.

4

3 回答 3

1

您将在此处找到如何使用 jQuery 和 JavaScript 设置全局变量的答案:

如何在 JavaScript 中声明一个全局变量?

jQuery - 定义一个全局“命名空间”,如下所示:

$.miniMenu = new Object();

JavaScript - 定义一个全局变量:

window.globalVar = "This is global!";
于 2012-11-19T04:33:13.497 回答
1

重要的是要注意,您要解决的问题不是 jQuery 的本意或设计目的。jQuery 主要是关于 DOM 操作(尽管它已经扩展了一些)。但是,还有许多其他库可以与 jQuery 结合使用来完成您想做的事情 - 主要是KnockoutJS。来自 Knockout 的网站:“使用简洁易读的语法轻松地将 DOM 元素与模型数据相关联”

此外,如果您只想在客户端存储数据,我建议您查看 localStorage 和 sessionStorage。Web 编程的这两个方面都是针对您面临的特定问题而设计的。但是,请注意您存储的数据,因为这可能成为数据访问点。

于 2012-11-19T04:08:34.100 回答
1

为此,您需要通过 ajax 等在服务器端检查单击按钮发出 ajax 请求并根据用户返回响应。如果您只做客户端,则可以轻松更改

您还可以使用会话/cookie 来放置有关用户的信息,并仅根据用户显示您可以设置会话

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

}

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

}

上述方法将是更好更容易的方法

像这里一样,所以他们不会为声誉低于 10k 的用户显示删除选项

于 2012-11-19T04:08:25.193 回答