Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing list as multiple parameter URL in snap

Is it possible to pass list parameter from browser to a handler function in Snap? How do I construct a multiple parameters URL from a list and send it to a handler function?

For instance, I need to delete some table rows or any other objects. I can not do it with the usual REST route:

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

simply because there could be too many and deleting 100 rows one by one can get a bit tedious.

I chose the doomed objects via checkbox input, say [3,4,6,8] rows need to be deleted. So how do I pass that list to the handler within URL and what would route look like for the action ?

UPDATE

Well, I finally did it with jquery and ajax call. Snap's "getParams" function can process multiple parameters URL but I still cannot figure out how to actually construct the URL without jquery and ajax.

I used javascript to collect the items to be deleted and build the array of the items. I then used ajax to construct multiple parameters URL and send it to the handler. Few things to note with this method and Snap:

-- Snaps's "getParams" function only supports old style multiple parameters URL:

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

and not the new one:

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

which makes passing complex parameters impossible.

-- The route should be:

("/objects/", method DELETE deleteObject)

and not the:

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

I did not answer my question because I don't believe it is the only way to pass multiple parameters URL with snap.

Although "getParams" can process it, my question still stays: how do I construct the URL and send it off to a handler?

For instance, Rails uses "link_to" function within view logic to construct the URL. Snap does not use any logic inside templates so how does it work then? It just can't be that the only way to pass multiple parameters URL in snap is with the help of javascript...?

Please someone confirm this for me?

like image 688
r.sendecky Avatar asked Jun 29 '13 13:06

r.sendecky


1 Answers

You're 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>

...gets submitted like this.

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

Then, inside your handler, this will get you a list of ByteStrings.

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

So this doesn't require JavaScript at all. But it works with JavaScript as well. If you use jQuery to submit a list like this...

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

...then you'll have to make an adjustment for the brackets

rs <- getsRequest (rqParam "rows[]")
cs <- getsRequest (rqParam "cols[]")
like image 102
mightybyte Avatar answered Nov 10 '22 04:11

mightybyte