source: Dev/branches/jQueryUI/client/js/api.js @ 249

Last change on this file since 249 was 249, checked in by hendrikvanantwerpen, 13 years ago

This one's for Subversion, because it's so close...

First widget (stripped down sequencer).
Seperated client and server code in two direcotry trees.

File size: 6.4 KB
Line 
1$(function() {
2
3    jQuery.extend({
4        getScript: function(url, callback) {
5            var d = $.Deferred();
6            if (callback)
7                d.done(callback);
8            var head = document.getElementsByTagName("head")[0];
9            var script = document.createElement("script");
10            script.src = url;
11
12            // Handle Script loading
13            {
14                var done = false;
15
16                // Attach handlers for all browsers
17                script.onload = script.onreadystatechange = function(){
18                    if ( !done && (!this.readyState ||
19                        this.readyState == "loaded" || this.readyState == "complete") ) {
20                        done = true;
21                        d.resolve();
22
23                        // Handle memory leak in IE
24                        script.onload = script.onreadystatechange = null;
25                    }
26                };
27            }
28
29            head.appendChild(script);
30
31            // We handle everything using the script element injection
32            return d;
33        }
34    });
35
36    rft = {
37        api: {
38            post: function(action,args) {
39                req = {
40                    action:action,
41                    args:args
42                }
43                user = rft.auth.getUser();
44                if ( user ) {
45                    req.uid = user.uid
46                }
47                return $.ajax({
48                    type: "POST",
49                    url: "../server/api.php",
50                    contentType: "application/json",
51                    data: JSON.stringify(req)
52                });
53            },
54            createObject: function(type, args) {
55                var d = $.Deferred();
56                rft.api.post("createObject",
57                {
58                    type: type,
59                    values: args
60                })
61                .done(function(data, textStatus, jqXHR) {
62                    d.resolve(data.uid);
63                })
64                .fail(function(jqXHR,textStatus,errorThrown){
65                    d.reject();
66                });
67                return d.promise();
68            },
69            getObject: function(type, uid, success) {
70                var d = $.Deferred();
71                rft.api.post("getObject",
72                {
73                    type: type,
74                    uid: uid
75                })
76                .done(function(data, textStatus, jqXHR) {
77                    d.resolve(data.object);
78                })
79                .fail(function(jqXHR,textStatus,errorThrown){
80                    d.reject();
81                });
82                return d.promise();
83            },
84            getObjects: function(type, predicates, success) {
85                var d = $.Deferred();
86                rft.api.post("getObjects",
87                {
88                    type: type,
89                    predicates: predicates
90                })
91                .done(function(data, textStatus, jqXHR) {
92                    d.resolve(data.objects);
93                })
94                .fail(function(jqXHR,textStatus,errorThrown){
95                    d.reject();
96                });
97                return d.promise();
98            },
99            deleteObject: function(type, uid, success) {
100                var d = $.Deferred();
101                d.reject();
102                return d.promise();
103
104            }
105        },
106        auth: new (function() {
107            var self = this;
108            var currentUser = null;
109           
110            self.login = function(username,password,success,error) {
111                var d = $.Deferred();
112                rft.api.post("login",
113                {
114                    username:username,
115                    password:password
116                })
117                .done(function(data, textStatus, jqXHR) {
118                    currentUser = data;
119                    d.resolve(data);
120                })
121                .fail(function(jqXHR,textStatus,errorThrown){
122                    d.reject();
123                });
124                return d.promise();
125            };
126           
127            self.register = function(username,password,success,error) {
128                var d = $.Deferred();
129                rft.api.post("register",
130                {
131                    username:username,
132                    password:password
133                })
134                .done(function(data, textStatus, jqXHR) {
135                    currentUser = data;
136                    d.resolve(data);
137                })
138                .fail(function(jqXHR,textStatus,errorThrown){
139                    d.reject();
140                });
141                return d.promise();
142            };
143           
144            self.getUser = function(){
145                return currentUser;
146            };
147        })(),
148        content: new (function() {
149            var self = this;
150            var current = "";
151           
152            function init() {
153                $(window).bind( 'hashchange', function(e){
154                    var url = $.bbq.getState("content");
155                    self.goTo(url);
156                });
157            }
158           
159            self.initial = function(location) {
160                if ( current ) {
161                    self.goTo(location);
162                }
163                var url = $.bbq.getState("content");
164                self.goTo(url || location);
165            }
166           
167            self.goTo = function(location) {
168                if ( current == location ) {
169                    return $.when();
170                }
171                current = location;
172                var d = $.Deferred();
173                $.get("pages/"+location+"/"+location+".html")
174                .fail(function(){
175                    $("#content").html("Page "+location+" not found.");
176                    d.reject();
177                })
178                .always(function(){
179                    $.bbq.pushState({
180                        "content": location
181                    });
182                })
183                .pipe(function(html){
184                    $("#content").html(html);
185                    return $.getScript("pages/"+location+"/"+location+".js")
186                })
187                .done(function(){
188                    d.resolve();
189                })
190                .fail(function(){
191                    d.reject();
192                })
193                return d;
194            }
195           
196            init();
197        })()
198    };
199
200    $.getScript("widgets/sequencer/sequencer.js");
201
202});
Note: See TracBrowser for help on using the repository browser.