BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / accountSystem.js

accountSystem.js

better userimage loading
timfa Tim Falken · pushed by timfa · 8 years ago
9b22d4f7 History
accountSystem.js 2.3 KiB Download Raw
1var AccountSystem =
2{
3 findByFullName: function(name)
4 {
5 var url = "http://timfalken.com/account/?action=find&query=" + name;
6
7 this.getJSON(url, function(status, response)
8 {
9 context = context || this;
10
11 callback && callback.call(this, response);
12 })
13 },
14
15 loadUserImage: function(user, callback, context)
16 {
17 var avatarImage = new image(user.avatar + ".png");
18
19 avatarImage.g_image.addEventListener('load', function()
20 {
21 context = context || this;
22
23 callback && callback.call(this, avatarImage);
24 });
25
26 avatarImage.g_image.addEventListener("error", function(error)
27 {
28 console.error(error)
29 });
30 },
31
32 login: function(email, password, callback, context)
33 {
34 var url = "http://timfalken.com/account/?action=login&email=" + email + "&password=" + btoa(password);
35 this.getJSON(url, function(status, response)
36 {
37 context = context || this;
38
39 callback && callback.call(this, response);
40 })
41 },
42
43 register: function(name, email, password, callback, context)
44 {
45 var url = "http://timfalken.com/account/?action=register&username=" + name + "&email=" + email + "&password=" + btoa(password);
46 this.getJSON(url, function(status, response)
47 {
48 context = context || this;
49
50 callback && callback.call(this, response);
51 })
52 },
53
54 setData: function(email, password, data, callback, context)
55 {
56 var url = "http://timfalken.com/account/?action=updatedata&key=emVybzIxMTI=&email=" + email + "&password=" + btoa(password) + "&newdata=" + btoa(JSON.stringify(data));
57 this.getJSON(url, function(status, response)
58 {
59 context = context || this;
60
61 callback && callback.call(this, response);
62 })
63 },
64
65 getJSON: function(url, callback) {
66 var xhr = new XMLHttpRequest();
67 xhr.open('GET', url, true);
68 xhr.responseType = 'json';
69 xhr.onload = function() {
70 var status = xhr.status;
71 if (status === 200) {
72 callback(null, xhr.response);
73 } else {
74 callback(status, xhr.response);
75 }
76 };
77 xhr.send();
78 }
79}