开发者

How to prevent the default behavior on internal linked pages in jQueryMobile?

开发者 https://www.devze.com 2023-03-16 02:11 出处:网络
How can I prevent the default behavi开发者_Python百科or on internal linked pages? My goal is to prevent the user from accessing page contents without first authenticate.Recently I was facing the sam

How can I prevent the default behavi开发者_Python百科or on internal linked pages?

My goal is to prevent the user from accessing page contents without first authenticate.


Recently I was facing the same situation. I was using jQuery Mobile along with jQuery Templates, all sensitive data coming from a web service.

My solution was to register a callback with the pagebeforeshow event for any page that the user needs authorization for. Within this callback function, I checked if the user is authorized, in my case this is a JSONP web service call, which either returns the data, or an appropriate error code.

Then, if the user is authorized, the data is rendered with jQuery Templates; otherwise I redirect the user to a login page, using $.mobile.changePage().


In beta3, you can make use of the "Dynamically Injecting Pages" technique as described here:

http://jquerymobile.com/test/docs/pages/page-dynamic.html


User authentication is a server-side thing and can't be implemented in javascript!

What stops me from sending a GET request myself then?

Pages that are not accessible without login should redirect to a login page if user is not authenticated. If you use web services directly from javascript, then they should return errors if the user is not authenticated.

Blocking access to pages in javascript is ridiculous. [But I have to admit that I've seen it before in commercial products...]

edit

Ok, to answer the question...

It depends on how the behaviours are attached to the page. If you use live with pageshow or pagecreate for those behaviours - then you can use a pagebefore* event and return false

another edit

I seem to understand the issue better. You have webservices and I will asume that they are secure and you don't want to make requests and get the 'error - not autenticated' response - there's no need to do so. And you use multiple pages in one html document.

With these assumptions I say you need to write a little wrapper over the $.mobile.changePage() - something like this:

$.mobile.changePageIf(condition,what,how,bool1,bool2){
if(condition){
  $.mobile.changePage(what,how,bool1,bool2);
  }else{
  //display "log in, dude!" or whatever
  }
}

And use the wrapper instead of the original function everywhere, putting a condition that returns true if you know user is logged in.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号