4f6192754b11499b7923d801d98f13b9e923b047
[ReadingsJQM.git] / js / app / BooksList.js
1 // Generated by CoffeeScript 1.3.3
2 (function() {
3   var slug_in_url;
4
5   slug_in_url = /([a-z0-9-]+)[/]?$/;
6
7   Readings.Book = (function() {
8
9     function Book(record) {
10       this.url = record.url;
11       this.href = record.href;
12       this.cover = record.cover;
13       this.author = record.author;
14       this.title = record.title;
15       this.slug = slug_in_url.exec(this.href)[1];
16     }
17
18     Book.prototype.render = function() {
19       return "<li><a href=\"reader.html?slug=" + this.slug + "\">" + this.title + "</a></li>";
20     };
21
22     Book.prototype.group = function() {
23       return this.author;
24     };
25
26     return Book;
27
28   })();
29
30   $.fn.Readings.BookList = function(category, tag) {
31     return this.each(function() {
32       var list;
33       $('[data-role=header] h1').text(tag.name);
34       list = $('[data-role=listview]', this);
35       return $.ajax({
36         url: "" + tag.href + "books/",
37         contentType: "json",
38         success: function(data) {
39           var b, books, last_separator, separator, show_separator, _i, _len, _results;
40           console.log(data);
41           books = $.map(data, function(rec) {
42             return new Readings.Book(rec);
43           });
44           list.empty();
45           last_separator = null;
46           show_separator = !(category === 'authors');
47           _results = [];
48           for (_i = 0, _len = books.length; _i < _len; _i++) {
49             b = books[_i];
50             if (show_separator) {
51               separator = t.group();
52               if (last_separator !== separator) {
53                 list.append("<li data-role=\"list-divider\">" + separator + "</li>");
54                 last_separator = separator;
55               }
56             }
57             list.append(b.render());
58             _results.push(list.listview('refresh'));
59           }
60           return _results;
61         }
62       });
63     });
64   };
65
66 }).call(this);