Main route with state

Some states are easily mapped to routes. For instance:

'users/:id' --> #users/123 

The state here is a prime integer, and therefore it is quite easy to create a reasonable route and pass that state to the route handler.

But what strategy should be used when your state is much more complicated (for example, a JSON object, an array of arrays, etc.)?

I notice that Gmail is "tokenizing" its state in what appears to be a base-64 encoded token, for example. something like that:

 #inbox/h223r488v8vHh4fa9a9qyUTkmb9334mN9O2s8 

... and then I imagine that it somehow changes this token back to a meaningful state, but this process is what I got confused about.


Relevant: http://lostechies.com/derickbailey/2011/12/27/the-responsibilities-of-the-various-pieces-of-backbone-js/

+4
source share
1 answer

Why not use the base64 javascript library? There is a ton. Here is one . Pretty sure you'll need JSON.stringify before encoding and JSON.parse .

Then you simply decode it if it is already in the url. Then you do not need to store it, you can simply decode and encode.

  routes: { 'encoded/:code': 'decode' }, decode: function(code) { // using the linked library var decoded = window.atob(code); // do what you want } 

base64 is built into some browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding

+1
source

Source: https://habr.com/ru/post/1490734/


All Articles