简体   繁体   中英

Best Practise for passing variables between web pages with KnockoutJS/Jquery

I am writing a simple web application in knockoutJS with Jquery. If you imagine it will be made up of 3 main pages (there are lots more but lets keep it simple):

  • Login page
  • Customer page
  • Previous Orders page

Each page contains different javascript includes, and have different CSS styles. Originally I was using ASP MVC for this and the routes would be like:

  • /login
  • /customer/{customerId}
  • /customer/{customerId}/Orders/

Now that I am purely using KnockoutJS the pages are actually:

  • login.html
  • customer-homepage.html
  • customer-orders.html

Now here is the crux of the problem, when I login. I contact a web service (the old asp mvc stuff exposed as JSON) and then want to redirect the user to the customer page, but want them to have knowledge of the customerId.

So once the ajax request from login returns with the valid users Id, I would then need to somehow redirect to the next page, with the variable. I was thinking along the lines of:

location.href = "customer-homepage.html?customerId=1";

However it feels a little dirty and I dont know if this sort of approach is good for SEO purposes, as most SEO focus seems to be on urls, so it should ideally look like the asp mvc route.

This led me onto thinking about maybe doing it as a one page app, but using the hashbang to redirect, as I would like to be able to swish between pages rather than do a complete post back. However I would need to unload some of the JS/CSS from memory and then load in newer stuff.

I know this is quite a wooly question, but from what I can see my options are either:

  • Use redirect requests with querystring data
  • Use one page application which would be GREAT, but would need to solve the unloading of existing resources (JS/CSS) in memory

I'm building a similar web app, and I personally think it would be a good separation of application logic if you still have three pages. All that knockout is doing is basically making your view(the html) much dumber(as MVVM recommends). And empowers your view to take over the interactions of the user.

But your suggestion seems to be analogous to having one view with different viewmodels, which isn't forbidden, but I think it would be harder to debug such an app.

To feed info into another page just do a POST with the customerId and then your server-side logic can use that info to do whatever the app requires of it.

I personally think that would solve all your problems, but then again, this may not be an answer but an opinion, you decide:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM