I was having problems with putting web2py tags inside my util.js, so I 
moved it to the views instead:


function doPost(callback, data) {
 $.post(callback, data, function(data){})
 .done(function() { alert("Done!"); })
 .fail(function() { alert("Failed!");})
}


function add_item(url, item_id, some_detail) {
 var test = {};
 test.item_id = item_id;
 test.some_detail = some_detail;
 
 var dat = {'test' : test};
 var data = $.toJSON(dat);


 doPost(url, data);
}

My button looks like this now:

<a href="" id="additem" onclick="add_item({{=URL('default', 'add_item')}}, 
{{item.id}}, 1);">Add Me</a>

But for some reason the add_item() in util.js is not being called.

On Monday, May 6, 2013 12:49:53 PM UTC-4, [email protected] wrote:
>
> Hi all,
>
> I'm trying to pass some data from javascript in the view back to the 
> controller using json. I'm unsure how to do this correctly. I created a 
> util.js:
>
> function doPost(callback, data) {
>  $.post(callback, data, function(data){})
>  .done(function() { alert("Done!"); })
>  .fail(function() { alert("Failed!");})
> }
>
>
> function add_item(item_id, some_detail) {
>  var test = {};
>  test.item_id = item_id;
>  test.some_detail = some_detail;
>  
>  var dat = {'test' : test};
>  var data = $.toJSON(dat);
>
>
>  doPost({{=URL('default', 'add_item')}}, data);
> }
>
> My default.py controller:
>
>
> def add_item():
>      data = gluon.contrib.simplejson.loads(request.body.read())
>      // Do insert to db
>      print data
>      return dict()
>
>
> My add button in the view:
>
> <div>
>     <{{=A('Add Me')}}
> </div>
>
>
>
> But I'm not sure how to tie this all together. The anchor tag needs an 
> onclick which points to the function add_item() in util.js. The util.js 
> should execute that function and do a post to the web2py controller, which 
> would then do an insert into the database. Could someone help me out? Or is 
> this not the ideal way of handling javascript data -> web2py?
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to