public
Last active

ROR link_to_remote clone. simple django ajax

  • Download Gist
README
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
django link_to_remote document:
ajax+param Example:
{% ajax %}
link_to_remote
url => /dashboard/serverstatus/
update => #server_status_body
data => maxcount=10
{% endajax %}
ajax+update div Example:
{% ajax %}
link_to_remote
url => /dashboard/server/
update => #server
{% endajax %}
click ajax Example:
{% ajax %}
link_to_remote
condition => $("#serverstatus_more").click
url => /dashboard/serverstatus/
update => #server_status_body
data => maxcount=15
success => if(msg!="")$("#serverstatus_more").css("display","")
{% endajax %}
ajaxtag.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
# -*- coding: utf-8 -*-
# GitHub http://gist.github.com/3247
from django import template
from django.template import resolve_variable
 
register = template.Library()
 
'''
ROR link_to_remote clone
 
Example 1:
{% ajax %}
link_to_remote
url => /dashboard/serverstatus/
update => #server_status_body
data => maxcount=10
{% endajax %}
 
Example 2:
{% ajax %}
link_to_remote
url => /dashboard/server/
update => #server
{% endajax %}
 
Example 3:
{% ajax %}
link_to_remote
condition => $("#serverstatus_more").click
url => /dashboard/serverstatus/
update => #server_status_body
data => maxcount=15
success => if(msg!="")$("#serverstatus_more").css("display","")
{% endajax %}
'''
 
def parseAjaxCmd(output):
"""parse ajax command"""
ajax_template={'link_to_remote':'''
<script>
%(condition)s(function(){
$.ajax({
type:'GET',
url:'%(url)s',
dataType:'html',
data: "%(data)s",
success: function(msg){
$("%(update)s").html(msg)
%(success)s
}
})
})
</script>
'''}
output=filter(lambda x:x!='',
map(lambda x:x.strip(),
output.split('\n')
)
)
ajaxcmd,params=output[0],output[1:]
tmpl=ajax_template.get(ajaxcmd,'')
if tmpl=='':return ''
param_dict={'condition':'$(document).ready',
'url':'/','data':'','update':'results',
'success':''}
param_dict.update(
dict( [(k.strip(),v.strip()) for k,v in [p.split("=>") for p in params]] )
)
print "param_dict",param_dict
script=tmpl%param_dict
print script
return script
 
class AjaxTagsNode(template.Node):
def __init__(self,nodelist):
self.nodelist = nodelist
def render(self, context):
output = self.nodelist.render(context)
return parseAjaxCmd(output)
 
@register.tag('ajax')
def ajaxtag(parser, token):
"""
{% ajax %}
{% endajax %}
"""
nodelist = parser.parse(('endajax',))
parser.delete_first_token()
return AjaxTagsNode(nodelist)
demo.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<html>
<head></head>
<body>
{% load ajaxtag %}
 
<div id="server_status_body">
</div>
 
<div id="serverstatus_more">
</div>
 
{% ajax %}
link_to_remote
url => /dashboard/serverstatus/
update => #server_status_body
data => maxcount=15
success => if(msg!="")$("#serverstatus_more").css("display","")
{% endajax %}
 
 
<a href="#box_server_more" id="serverstatus_more">more...</a>
{% ajax %}
link_to_remote
condition => $("#serverstatus_more").click
url => /dashboard/serverstatus/
update => #server_status_body
data => maxcount=15
{% endajax %}
 
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.