-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
82 lines (60 loc) · 1.71 KB
/
Copy pathexample.js
File metadata and controls
82 lines (60 loc) · 1.71 KB
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
/**
* Module dependencies.
*/
var query = require('query')
, dom = require('dom')
, domify = require('domify')
, Emitter = require('emitter')
, dynamicRows = require('dynamic-rows');
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 model = new Emitter({ name: 'Test value' });
var rows = dynamicRows(query('#rows'), template);
/**
* Register click events for buttons
*/
// add new entry
query('#add').onclick = function(){
var index = parseInt(query('#indexValue').value)
, el;
el = rows.insert(index, model);
initWidget(el);
};
// 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;
model.emit('change name', value);
};
/**
* Bind the 'remove' and 'add' events of a widget.
*/
function initWidget(el) {
// add button
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
initWidget(rows.append(index, model));
};
// remove button
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);
};
}