This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
simple.js
449 lines (404 loc) · 15.5 KB
/
simple.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Simple.js
//
// A simplistic MV* JavaScript library.
// Simple may be freely distributed under the MIT license.
//
// Created by Kim Joar Bekkelund <[email protected]>
//
// The primary function of *Simple.js* is to create simple abstractions for
// models and views. It aims to be a JavaScript MV* library which is both easy
// to understand and easy to extend.
//
// Simple.js is (currently) 123 [thoroughly tested](https://github.com/kjbekkelund/simple/blob/master/spec/simple-spec.js)
// lines of code. The project is [hosted on Github](https://github.com/kjbekkelund/simple),
// where you can also find the [changelog](https://github.com/kjbekkelund/simple/blob/master/CHANGELOG.md).
//
// Simple.js depends on [jQuery](http://jquery.com/) 1.7.0. Remember to include
// it before you include Simple.js itself.
//
// You can install Simple.js using Bower: `bower install --save simple.js`
//
// This library is heavily inspired by [Backbone.js](http://backbonejs.org/)
// and [Spine.js](http://spinejs.com/).
(function (root, factory) {
// Export magic
// ------------
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but only CommonJS-like
// enviroments that support module.exports, like Node.
module.exports = factory(require('jquery'));
} else {
// Browser globals (root is window)
root.Simple = factory(root.jQuery);
}
}(this, function ($) {
// Namespace
// ---------
// The top-level namespace. All public Simple classes and modules will be
// attached to this.
var Simple = {};
// Events
// ------
// A module that can be *mixed in* to *any* object in order to provide it with
// custom events:
//
// var object = {};
// $.extend(object, Simple.Events);
//
// You may bind events with with `on`, unbind with `off`, and fire all
// event callbacks in successtion with `trigger`:
//
// object.on('test', function(){ console.log("testing!"); });
// object.trigger('test');
// object.off('test');
var Events = Simple.Events = {
// **Bind an event to a callback**
//
// - `event` is the name of the event to bind
// - `callback` is the function which is called when the event is triggered
// - `context` (optional) is the scope for the callback, i.e. the
// meaning of `this` in the callback
on: function(event, callback, context) {
var callbacks = this._callbacks || (this._callbacks = {});
var events = callbacks[event] || (callbacks[event] = []);
events.push({ callback: callback, context: context });
},
// **Unbind an event**
//
// - `event` is the name of the event to unbind
// - `callback` (optional) is the function which was bound
// - `context` (optional) is the scope the event must have to be removed
off: function(event, callback, context) {
// If neither callback nor context is specified we remove all
// callbacks for an event.
if (!callback && !context) {
delete this._callbacks[event];
}
// If there are callbacks specified for an event, we loop through
// them and remove the callback if
//
// * callback and context matches
// * callback matches and context is not specified
// * callback is not specified and context matches
var events = this._callbacks[event] || [];
for (var i = 0; i < events.length; i++) {
if (!(callback && events[i].callback !== callback || context && events[i].context !== context)) {
events.splice(i, 1);
}
}
},
// **Trigger an event**
//
// The first argument is the name of the event to trigger, all the
// following (optional) arguments will be passed to the bound callback.
//
// This means that an event that is triggered like this
//
// model.trigger("test", "Kim Joar")
//
// ... can receive the second argument if we have bound the event like this:
//
// model.on("test", function(name) {
// console.log(name) // "Kim Joar"
// });
//
trigger: function(event) {
var args = Array.prototype.slice.call(arguments, 1);
var callbacks = this._callbacks || {};
var events = callbacks[event] || [];
for (var i = 0; i < events.length; i++) {
events[i].callback.apply(events[i].context || this, args);
}
}
};
// **Add events to Simple namespace**
//
// We can now bind events with
//
// Simple.events.on(...)
//
// unbind events with
//
// Simple.events.off(...)
//
// and trigger events with
//
// Simple.events.trigger(...)
Simple.events = $.extend({}, Events);
// Views
// -----
//
// You should start by reading
// [A view’s responsibility](http://open.bekk.no/a-views-responsibility/)
// to understand the philosophy these views are based on.
// Create a new view. This constructor is called whenever a view is
// initialized, so we can use it set up some basic state that is common
// among all views.
var View = Simple.View = function(options) {
// We always expect to receive the view's `el`, so we just set it
// right away.
this.el = options.el;
// All events specified in the `events` hash will be delegated when the
// view is initialized.
this.delegateEvents();
// On initialization the input is passed through to the `initialize`
// method, which can be overriden when creating new views.
this.initialize(options);
};
// Attach all inheritable methods to the View prototype.
//
// We also attach events which means that we can now bind, unbind and
// trigger events on a view, e.g.
//
// var view = new Simple.View();
// view.on(...)
// view.off(...)
// view.trigger(...)
$.extend(View.prototype, Events, {
// no-op initialize
initialize: function() {},
// **View rendering**
//
// `render` is responsible for populating the view's HTML element.
// The default implementation is a no-op, which means that:
//
// 1. A view must override this function with its specific view
// rendering implemenation.
// 2. Simple.js works with whatever HTML templating method you like (as
// long as it updates `view.el`, of course).
//
// A simple example of an overridden render when using
// [Mustache](http://mustache.github.com/):
//
// render: function() {
// var template = "<h1>Hi {{name}}</h1>";
// var data = {
// name: "Kim Joar"
// };
//
// this.el.html(Mustache.to_html(template, data));
// }
//
render: function() {},
// **DOM lookup**
//
// jQuery delegate for element lookup, scoped to DOM elements within
// the current view. You can think of `view.$` as a scoped jQuery
// lookup. Example:
//
// var view = new Simple.View({ el: $(".user") });
//
// // instead of
// $(".user form")
//
// // we should now do
// view.$("form")
$: function(selector) {
return this.el.find(selector);
},
// **Event delegation**
//
// Set callbacks, where `this.events` is a hash of<br>
// `{"event selector": "callback"}`-pairs. For example:
//
// {
// 'mousedown .title': 'edit',
// 'click .button': 'save'
// }
//
// To specify an event directly on `el`, leave the selector blank:
//
// {
// 'submit': 'save'
// }
//
// Callbacks will be bound to the view, with `this` set properly.
// Uses event delegation for efficiency.
delegateEvents: function() {
if (!this.events) return;
for (var key in this.events) {
var methodName = this.events[key],
method = $.proxy(this[methodName], this),
match = key.match(/^(\w+)(:?\s+(.*))?$/),
eventName = match[1],
selector = match[2];
this.el.on(eventName, selector, method);
}
}
});
// Models
// ------
// Create a new model. This constructor is called whenever a model is
// initialized, so we can use it set up some basic state that is common
// among all models.
var Model = Simple.Model = function(options) {
// The model's attributes default to the to those specified in the
// `defaults` hash, e.g. as follows:
//
// var Model = Simple.Model.extend({
// defaults: {
// name: "Kim Joar"
// }
// });
this.attributes = this.defaults || {};
// We set the attributes specified when initializing the model, or an
// empty hash if none is specified.
this.attrs(options || {});
// On initialization the input is passed through to the `initialize`
// method, which can be overriden when creating new models.
this.initialize(options);
};
// Attach all inheritable methods to the Model prototype.
//
// We also attach events which means that we can now bind, unbind and
// trigger events on a model, e.g.
//
// var model = new Simple.Model();
// model.on(...)
// model.off(...)
// model.trigger(...)
$.extend(Model.prototype, Events, {
// no-op initialize
initialize: function() {},
// **Perform an Ajax GET request**
//
// Will trigger the event `fetch:started` when starting. On success or
// failure either an event is triggered or a callback is executed if
// one is passed in the options hash.
//
// - Success: The event `fetch:finished` or the `success` callback
// - Failure: The event `fetch:error` or the `error` callback
//
// On success the received properties are always set on the model,
// regardless of whether event or callback is performed.
//
// Example with success callback:
//
// var model = new Simple.Model();
// model.fetch({
// success: function(data) {
// // we have a success
// }
// });
//
// Fetch returns the [jQuery XMLHttpRequest object](http://api.jquery.com/jQuery.ajax/#jqXHR),
// `jqXHR`, which implements the Promise interface.
//
// Example using promises:
//
// var model = new Simple.Model();
// this.model.fetch().then(function(data, textStatus, jqXHR) {
// // we have a success
// });
fetch: function(options) {
return this._performRequest("fetch", this, options || {}, {});
},
// **Perform an Ajax POST request**
//
// Will trigger the event `save:started` when starting. On success or
// failure either an event is triggered or a callback is executed if
// one is passed in the options hash.
//
// - Success: The event `save:finished` or the `success` callback
// - Failure: The event `save:error` or the `error` callback
//
// On success the received properties are always set on the model,
// regardless of whether event or callback is performed.
//
// Example with success callback:
//
// var model = new Simple.Model();
// model.save({
// success: function(data) {
// // we have a success
// }
// });
//
// Save returns the [jQuery XMLHttpRequest object](http://api.jquery.com/jQuery.ajax/#jqXHR),
// `jqXHR`, which implements the Promise interface.
//
// Example using promises:
//
// var model = new Simple.Model();
// this.model.save().then(function(data, textStatus, jqXHR) {
// // we have a success
// });
save: function(options) {
return this._performRequest("save", this, options || {}, {
type: "POST",
data: JSON.stringify(this.attributes),
contentType: 'application/json'
});
},
// Helper for AJAX requests
_performRequest: function(type, model, options, additionalParams) {
this.trigger(type + ':started');
var params = {
url: (typeof model.url === "function") ? model.url() : model.url,
dataType: options.dataType || model.dataType || "json",
success: function(data) {
model.attrs(data);
if (typeof options.success !== "undefined") {
options.success(data);
} else {
model.trigger(type + ':finished');
}
},
error: function(jqXHR, resp) {
if (typeof options.error !== "undefined") {
options.error();
} else {
model.trigger(type + ':error', resp);
}
}
};
return $.ajax($.extend(params, additionalParams));
},
// **Attributes**
//
// Set or get an attribute
attr: function(name, value) {
if (typeof value === "undefined") {
return this.attributes[name];
} else {
this.attributes[name] = value;
}
},
// Set several or get all attributes
attrs: function(attributes) {
if (typeof attributes === "undefined") {
// Return a copy of the attributes as we want all adding and
// removing of attributes to go through `attr` and `attrs`.
return $.extend({}, this.attributes);
} else {
$.extend(this.attributes, attributes);
}
}
});
// Inheritance
// -----------
// Shared empty constructor function to aid in prototype-chain creation.
var ctor = function() {};
// Set up inheritance for the model and view.
View.extend = Model.extend = function(properties) {
var parent = this;
// Create child constructor
var child = function() {
// … which only job is to call the parent construtor with all
// the arguments
parent.apply(this, arguments);
};
// Set the prototype chain to inherit from `parent`
ctor.prototype = parent.prototype;
child.prototype = new ctor();
// Add prototype properties, i.e. instance properties
$.extend(child.prototype, properties);
// The child must also be able to create new subclasses
child.extend = parent.extend;
return child;
};
return Simple;
}));