what is an ajax callback and how do i use it? what is it for?

On Thursday, 17 January 2013 03:16:38 UTC, rochacbruno wrote:
>
> models/foo.py
>
> # the thing table
> db.define_table("thing", Field("name"))
>
> # you may want to store likes on another table so you never allow a user 
> to like the same thing twice
> db.define_table("thing_likes",
>     Field("user", "reference auth_user", notnull=True),
>     Field("thing", "reference thing", notnull=True),
>     Field("unikey", unique=True, notnull=True)
> )
>
> # this ensure that user cannot like a thing twice
> db.thing_likes.unikey.compute = lambda row: "%(user)s-%(thing)s" % row
>
>
> controllers/default.py
>
> def show_thing():
>      thing_id = request.args(0)
>      thing = db.thing[thing_id]
>      thing_likes = db(db.thing_likes.id == thing.id).count()
>      return dict(thing=thing. thing_likes=thing_likes)
>
> def like_thing():
>     thing_id = request.args(0)
>     user_id = auth.user_id
>     try:
>         db.thing_likes.validate_and_insert(
>             thing=int(thing_id),
>             user=int(user_id)
>         )
>         redirect(URL('show_thing', args=thing_id))
>     except:
>         return "thing cannot be liked twice"
>             
>
> views/default/show_thing.html
>
> <h1>{{=thing.name}}</h1>
> <a href="{{=URL('like_thing', args=thing.id)}}"> Like this thing </a>
>
>
> ========================
>
> To a better implementation you should use an ajax callback to the "like" 
> button, and also you should check if user already likes to show a different 
> button, maybe unlike.
>

-- 



Reply via email to