This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
62 lines (50 loc) · 1.65 KB
/
example.js
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
function init() {
//
// By default, load todo model anonymously
//
Daybed.SETTINGS.SERVER = "https://daybed.io/v1";
var hash = window.location.hash.slice(1) || 'daybed:examples:react:todo';
window.location.hash = hash;
var modelId = hash;
Daybed.SETTINGS.credentials = undefined;
//
// Instantiate Models and Views
//
var definition = new Daybed.Definition({id: modelId});
var recordList = new Daybed.RecordList(definition);
// Instantiate views
var listView = new Daybed.TableView({collection: recordList});
var formView = new Daybed.RecordFormView({definition: definition});
// Wait for definition to be fetched from server
// in order to populate form fields and list headers
definition.whenReady(function () {
$('#records-list').html(listView.render().el);
$('#form-holder').html(formView.render().el);
});
// Fetch definition !
definition.fetch();
// Fetch records !
recordList.fetch();
//
// Cable list and form events
//
// Rebuild the form with bound instance on edit.
listView.on('edit', function (record, row) {
$('tr.info').removeClass('info');
row.$el.addClass('info');
formView.setup(record);
formView.render();
});
// On creation, add to the collection.
formView.on('created', function (record) {
recordList.add(record);
});
// After cancel or edition, empty the form.
formView.on('cancel saved created', function () {
formView.setup(null);
formView.render();
$('tr.info').removeClass('info');
});
}
init();
window.onhashchange = init;