-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
74 lines (56 loc) · 1.67 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
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Module dependencies.
*/
var query = require('query')
, dom = require('dom')
, domify = require('domify')
, Model = require('model')
, ReactiveList = require('reactive-list');
var template = domify([
'<tr data-index="{index}">',
' <td>',
' <span>At {index}</span>',
' <input type="text" value="{name}">',
' <input class="add" type="button" value="+">',
' <input class="remove" type="button" value="-">',
' </td>',
'</tr>'
].join('\n'));
var User = Model('user').attr('name');
var model = new User({ name: 'Test value' });
var rows = ReactiveList(query('#rows'), template);
// remove button
rows.on('add', function(index, model, el){
query('.add', el).onclick = function(){
// the index needs to be recalculated since it could have been changed
var index = parseInt(dom(el).attr('data-index'));
// append after the current element
rows.append(index, model);
};
query('.remove', el).onclick = function(){
// the index needs to be recalculated since it could have been changed
var index = parseInt(dom(el).attr('data-index'));
// remove the current element
rows.remove(index);
};
});
/**
* Register click events for buttons
*/
// add new entry
query('#add').onclick = function(){
var index = parseInt(query('#indexValue').value)
, el;
if (Number.isNaN(index)) return rows.append(model);
rows.insert(index, model);
};
// remove entry
query('#remove').onclick = function(){
var index = parseInt(query('#indexValue').value);
if (Number.isNaN(index)) return;
rows.remove(index);
};
query('#change-value').onclick = function(){
var value = query('#new-value').value;
model.name(value);
};