Source

quantumcore.dynamicpage / quantumcore / dynamicpage / commands.py

import simplejson as json

class Commands(list):
    """a list of commands to be sent to the server"""
    
    def render_wsgi(self, environ, start_response, encoding="utf-8"):
        """return a WSGI response with the commands to perform on the client-side"""
        data = json.dumps(self).encode(encoding)
        
        headers = {'Content-Type': 'text/javascript',
                   'Content-Length' : str(len(data))}
        start_response("200 Ok", headers.items())
        return data,
        
        
###
### commands are simple functions which take input and reformat them into
### JSON compatible commands
###

def replace_inner(id_, content):
    return {'command': 'replace_inner',
            'payload': {'id' : id_, 'content' : content }
           }

def append(id_, content, slideDown = False, fadeIn = False):
    return {'command': 'append',
            'payload': {'id' : id_, 'content' : content,
                        'slideDown' : slideDown, 'fadeIn' : fadeIn}
           }

def remove(id_, content):
    return {'command': 'remove',
            'payload': {'id' : id_}
           }