This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtomvc.js
207 lines (175 loc) · 5.55 KB
/
tomvc.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
( function( window ) {
'use strict';
var ToMvc = {};
/*
Controller constructor
*/
var Controller = function( options ) {
this.defaults = {};
this.views = [];
this.models = [];
this.events = [];
merge.call( this, this.defaults, options );
};
/*
View constructor
*/
var View = function( options ) {
// Controller is necessary
if ( options && options.controller ) {
this.controller = options.controller;
} else {
throw "View constructor requires controller";
}
// DOM element is necessary
if ( options.el ) {
this.el = options.el;
} else {
throw "View constructor requires DOM element";
}
// Default options
this.defaults = {
listeners: []
};
// Extend defaults
merge.call( this, this.defaults, options );
this.setName( options.name );
this.controller.registerView( this.getName() );
};
/*
Model constructor
*/
var Model = function( options ) {
this.defaults = {
listeners: []
};
// Controller is necessary
if ( options.controller ) {
this.controller = options.controller;
} else {
throw "Model constructor requires controller";
}
merge.call( this, this.defaults, options );
this.setName( options.name );
this.controller.registerModel( this.getName() );
};
/*
Make OOP possible
*/
function extend() {
/*jshint validthis: true */
var parent = this; // ToMvc.Controller || ToMvc.View || ToMvc.Model
var extended = function( options ) {
parent.call( this, options ); // call super constructor.
};
// Do the OOP trick:
extended.prototype = Object.create( parent.prototype );
extended.prototype.constructor = extended;
return extended;
}
// Add extend option to all basic ToMvc methods
Controller.extend = View.extend = Model.extend = extend;
/*
Controller
*/
Controller.prototype.getViews = function() {
return this.views.slice();
};
Controller.prototype.getModels = function() {
return this.models.slice();
};
Controller.prototype.getEvents = function() {
return this.events.slice();
};
Controller.prototype.registerView = function( name ) {
this.views.push( name );
};
Controller.prototype.registerModel = function( name ) {
this.models.push( name );
};
Controller.prototype.addEvent = function( eventname, callback, controller ) {
this.controller.events.push( {
'eventname': eventname,
'callback': callback
} );
// console.info('events:', ToMvc.Controller.events);
};
Controller.prototype.triggerEvent = function( eventname, data ) {
this.events.forEach( function( event ) {
if ( event.eventname === eventname ) {
event.callback( data );
}
} );
};
/*
View
*/
View.prototype.getDefaultName = function() {
return 'view' + ( this.controller.getViews().length + 1 );
};
View.prototype.getName = function() {
return this.name;
};
View.prototype.setName = function( name ) {
this.name = name || this.getDefaultName();
};
View.prototype.listenTo = function( event, callback ) {
this.controller.addEvent.call( this, event, callback );
};
View.prototype.broadcast = function( event, data ) {
this.controller.triggerEvent( event, data );
};
/*
Model
*/
Model.prototype.init = function( name ) {
this.setName( name );
Controller.addModel( this.name );
};
Model.prototype.getDefaultName = function() {
return 'model' + ( this.controller.getModels().length + 1 );
};
Model.prototype.getName = function() {
return this.name;
};
Model.prototype.setName = function( name ) {
this.name = name || this.getDefaultName();
};
Model.prototype.listenTo = function( event, callback ) {
this.controller.addEvent.call( this, event, callback, this.lcontroller );
};
Model.prototype.broadcast = function( event, data ) {
this.controller.triggerEvent( event, data );
};
// Wrap up
ToMvc.version = '0.2.0';
ToMvc.Controller = Controller;
ToMvc.View = View;
ToMvc.Model = Model;
// Make ToMvc globally available
window.ToMvc = window.ToMvc || ToMvc;
///////////
// Utils //
///////////
// Based on http://youmightnotneedjquery.com/
// Extend functions to caller
// Extend all other options to given object
var merge = function( out ) {
out = out || {};
for ( var i = 1; i < arguments.length; i++ ) {
if ( !arguments[ i ] ) {
continue;
}
for ( var key in arguments[ i ] ) {
// If it is a function, make it a method of the current instance
if ( typeof arguments[ i ][ key ] === 'function' ) {
this[ key ] = arguments[ i ][ key ];
} else if ( arguments[ i ].hasOwnProperty( key ) ) {
// else it's just a property
out[ key ] = arguments[ i ][ key ];
}
}
}
return out;
};
}( typeof window === "undefined" ? global : window ) ); // window not available in node