Login

ROR link_to_remote clone. simple django ajax

Author:
qingfeng
Posted:
July 31, 2008
Language:
Python
Version:
.96
Score:
1 (after 3 ratings)

More: http://gist.github.com/3247

 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)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

youell (on August 5, 2008):

Hi. What exactly does this do? I'm not a Rails refugee but I'd like to understand. Thanks!

#

Please login first before commenting.