Ignore:
Timestamp:
Mar 13, 2011, 12:05:16 AM (13 years ago)
Author:
gav
Message:

Rework entry create and save ajax to allow multiple page element updates with JSON.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/web-app/js/application.js

    r825 r859  
    1919}
    2020
    21 function errorIndication() {
    22     return jQuery('#jQueryAjaxDefaultError').clone();
     21function errorIndication(jqXHR, textStatus, errorThrown) {
     22    return jQuery('#jQueryAjaxDefaultError').clone().append('Status:'+jqXHR.status+', '+textStatus+'.');
    2323}
     24
     25// Apply updates to multiple page elements.
     26// @json JSON response object from an ajax request.
     27// @json.updates Array of element updates to apply.
     28// @element.mode The update mode: execute or replace, prepend, append.
     29// @element.script Script to execute, if execute mode.
     30// @element.target jQuery target selector, if update mode.
     31// @element.content Content to update target with, if update mode.
     32function applyElementUpdates(json) {
     33    var updates;
     34    var script;
     35
     36    if(json.updates) {
     37        updates = json.updates;
     38        var element;
     39        var scripts = new Array();
     40
     41        for(element in updates) {
     42            element = updates[element];
     43
     44            switch(element.mode) {
     45                case 'execute':
     46                    scripts.push(element.script);
     47                    break;
     48                case 'replace':
     49                    jQuery(element.target).html(element.content);
     50                    break;
     51                case 'prepend':
     52                    jQuery(element.target).prepend(element.content);
     53                    break;
     54                case 'append':
     55                    jQuery(element.target).append(element.content);
     56                    break;
     57            }
     58        }
     59
     60        // Run scripts.
     61        for(script in scripts) {
     62            script = scripts[script];
     63            eval(script);
     64        }
     65
     66    } // if(json.updates)
     67} // applyElementUpdates
     68
     69
     70
Note: See TracChangeset for help on using the changeset viewer.