source: trunk/web-app/js/application.js

Last change on this file was 859, checked in by gav, 13 years ago

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

File size: 2.0 KB
Line 
1var Ajax;
2if (Ajax && (Ajax != null)) {
3    Ajax.Responders.register({
4    onCreate: function() {
5        if($('spinner') && Ajax.activeRequestCount>0)
6        Effect.Appear('spinner',{duration:0.5,queue:'end'});
7    },
8    onComplete: function() {
9        if($('spinner') && Ajax.activeRequestCount==0)
10        Effect.Fade('spinner',{duration:0.5,queue:'end'});
11    }
12    });
13}
14
15// jQuery AJAX utils.
16
17function loadingIndication() {
18    return jQuery('#jQueryAjaxLoading').clone();
19}
20
21function errorIndication(jqXHR, textStatus, errorThrown) {
22    return jQuery('#jQueryAjaxDefaultError').clone().append('Status:'+jqXHR.status+', '+textStatus+'.');
23}
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 TracBrowser for help on using the repository browser.