Passing a list as a URL of multiple parameters

Is it possible to pass a list parameter from a browser to a handler function in Snap? How to create a URL with several parameters from a list and send it to a handler function?

For example, I need to delete table rows or any other objects. I cannot do this with the usual REST route:

("/objects/:id", method DELETE deleteObject) 

simply because there can be too many, and deleting 100 lines one at a time can be a bit tedious.

I selected the doomed objects by entering a checkbox, say, [3,4,6,8] rows need to be deleted. So how do you pass this list to the handler in the url and what will the route for the action look like?

UPDATE

Ok, I finally did this with jquery and ajax call. The snap function "getParams" can handle multiple parameter URLs, but I still can't figure out how to actually build the URL without jquery and ajax.

I used javascript to collect elements to remove and create an array of elements. Then I used ajax to create the URL of several parameters and send it to the handler. A few things to note with this method and Snap:

- The "getParams" binding function supports only a few old-style URL parameters:

 "a=1&a=2&a=3&a=4" 

not new:

 "a[]=1&a[]=2&a[]=3&a[]=4" 

which makes it impossible to transfer complex parameters.

- The route should be:

 ("/objects/", method DELETE deleteObject) 

but not:

 ("/objects/:ids", method DELETE deleteObject) 

I did not answer my question because I do not believe that this is the only way to pass the URL of several parameters with a binding.

Although "getParams" can handle it, my question still remains: how do I create the URL and send it to the handler?

For example, Rails uses the link_to function in presentation logic to create a URL. Snap doesn't use any logic inside the templates, so how does it work? It simply cannot be that the only way to pass multiple parameter URLs in a snap is with javascript ...?

Please confirm this for me?

+6
source share
1 answer

You are pretty much there. The following form ...

 <form action="/foo"> <ul> <li>Row 1: <input type="checkbox" name="a" value="1"/></li> <li>Row 2: <input type="checkbox" name="a" value="2"/></li> <li>Row 3: <input type="checkbox" name="a" value="3"/></li> <li>Row 4: <input type="checkbox" name="a" value="4"/></li> <li>Row 5: <input type="checkbox" name="a" value="5"/></li> </ul> <input type="submit" name="submit" value="Submit"/> </form> 

... sent this way.

 http://localhost:8000/foo?a=2&a=3&a=5&submit=Submit 

Then inside your handler you will get a list of bytes.

 fooHandler = do as <- getsRequest (rqParam "a") 

Thus, JavaScript is not required for this. But it works with JavaScript too. If you use jQuery to send a list like this ...

 var fieldData = { rows: [0,1,4], cols: [2,3,5] }; $.getJSON('http://localhost:8000/foo', fieldData, ...); 

... then you will need to adjust the brackets

 rs <- getsRequest (rqParam "rows[]") cs <- getsRequest (rqParam "cols[]") 
+4
source

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


All Articles