summaryrefslogtreecommitdiffstats
path: root/web/js/labitrack.d/43-browse.js
blob: 14399ac7a62208b57bcf1db7306c69687f71ca69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function(){

	function hdl_add(){
		console.log('add');
	}

	function hdl_remove(){
		console.log('remove');
	}

	function hdl_reset(){
		console.log('reset');
		this.render();
	}

	var collection = Backbone.Collection.extend({
		model: λ.o,
		url: function(){
			return '/browse/'+(this.nextpage++)+'.json';
		},
		fetchpage: function(page){
			if (page) {
				this.nextpage = page;
			}
			this.fetch();;
		},
		comparator: function(object){
			return object.id;
		},
		parse: function(data){
			this.stats = {count: data.count};
			console.log(data);
			return data.objects;
		}
	});

	var browse = Backbone.View.extend({
		initialize: function() {
			var messages = this.collection;
			messages.bind("reset", hdl_reset, this);
			messages.bind("add", hdl_add, this);
			messages.bind("remove", hdl_remove, this);
		},
		render: function(page){
			if (page) this.page = page;
			page = this.page;
			var stats = this.collection.stats;
			var pages = [];
			if (stats !== undefined) {
				var pgcnt = Math.ceil(stats.count / 10);
				pages = λ.pagination(page, pgcnt);
			}
			var data = {
				rows: this.collection.toJSON(),
				pages: pages
			};
			console.log(data);
			$(this.el).html(λ.template('objecttable', data));
		}
	});

	var collection = new collection();

	var view = λ.routableview.extend({
		initialize: function() {
			λ.routableview.prototype.initialize.call(this);
			this.browse = new browse({collection: collection});
		},
		render: function (page) {
			page || (page = 1);
			page = parseInt(page, 10);
			console.log('page', page);
			λ.setcontent('browse', {page: page});
			this.browse.el = $(this.el).find('#objecttable_ph')[0];
			console.log(this.browse.render);
			this.browse.render(page);
			this.browse.collection.fetchpage(page);
		}
	});

	view.route('browse');
	view.route('browse/page/:page');
}());