-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (77 loc) · 1.82 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* viewcollection
* ViewCollection component
*
* @copyright 2012 Enrico Marino and Federico Spini
* @license MIT
*/
/*
* Expose `ViewCollection`
*/
module.exports = ViewCollection;
/*
* Module dependencies
*/
var View = require('view');
var ViewModel = require('viewmodel');
var Collection = require('collection');
/*
* ViewCollection
* Create a viewcollection.
*
* @param {Object} options options
* @param {Element} [options.el] element
* @param {Collection} [options.collection] collection
* @return {ViewCollection} a viewcollection
*/
function ViewCollection(options) {
if (!(this instanceof ViewCollection)) {
return new ViewCollection(options);
}
View.call(this, options);
this.collection = options.collection || new Collection([]);
this.collection.model = this.viewmodel;
this.listen(this.collection, this.messages);
this.viewmodels = {};
}
/**
* Inherit from `View`
*/
ViewCollection.prototype = Object.create(View.prototype);
ViewCollection.prototype.constructor = ViewCollection;
/*
* Define the viewmodel constructor
*/
ViewCollection.prototype.viewmodel = ViewModel;
/**
* messages
*/
ViewCollection.prototype.messages = {
"add": "onadd",
"remove": "onremove"
};
/*
* onadd
* Add a viewmodel to this viewcollection
* when a model is added to the binded collection
*
* @param {Model} model the added model
* @api public
*/
ViewCollection.prototype.onadd = function(model) {
var viewmodel = this.viewmodel({model: model});
var id = viewmodel.id;
var el = viewmodel.el;
this.viewmodels[id] = viewmodel;
this.el.appendChild(el);
};
/*
* onremove
* Remove a viewmodel from this viewcollection
* when its model is removed from the binded collection
*
* @param {Model} model the removed model
* @api public
*/
ViewCollection.prototype.onremove = function(model) {
};