Fix page loading scheme; fix User switching
[blerg.git] / www / jssrc / blerg / User.js
1 enyo.kind({
2         name: "blerg.User",
3         kind: "blerg.Pager",
4         listKind: "blerg.Record",
5         published: {
6                 username: "",
7                 permalink: false,
8                 record: null,
9         },
10         statics: {
11                 locationDetect: function(l) {
12                         var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(\d+))?$/);
13                         if (m) {
14                                 return {
15                                         kind: "blerg.User",
16                                         username: m[1],
17                                         permalink: m[2] != undefined,
18                                         record: parseInt(m[2])
19                                 };
20                         }
21                 }
22         },
23         create: function() {
24                 this.inherited(arguments);
25                 this.usernameChanged();
26         },
27         usernameChanged: function() {
28                 this.bubble('onSetTitle', {section: '@' + this.username});
29                 this.$.records.destroyComponents();
30                 this.lastRecord = null;
31                 this.$.loadMoreButton.hide();
32                 if (this.permalink) {
33                         this.loadItems(this.record, this.record);
34                 } else {
35                         this.loadMore();
36                 }
37         },
38         loadItems: function(from, to) {
39                 this.inherited(arguments);
40
41                 var url;
42                 if (from != undefined && to != undefined) {
43                         url = baseURL +  '/get/' + this.username + '/' + from + '-' + to;
44                 } else {
45                         url = baseURL +  '/get/' + this.username;
46                 }
47
48                 var req = new enyo.Ajax({
49                         url: url
50                 });
51                 req.response(function(inSender, inResponse) {
52                         if (this.permalink) {
53                                 this.$.loadMoreButton.hide();
54                         } else {
55                                 this.$.loadMoreButton.show();
56                         }
57
58                         for (var i = 0; i < inResponse.length; i++) {
59                                 inResponse[i].author = this.username;
60                         }
61
62                         this.addItems(inResponse);
63                 }.bind(this));
64                 req.go();
65         }
66 });