Skip to content

Commit a3ab5b3

Browse files
committed
Use a slightly modified version of Backbone's inherits as a generic means of adding prototypal inheritance.
1 parent 7c16d97 commit a3ab5b3

File tree

7 files changed

+36
-63
lines changed

7 files changed

+36
-63
lines changed

src/collection.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
}
1111
}
1212

13+
Collection.extend = function() {
14+
return Model.Utils.inherits(this)
15+
}
16+
1317
var methods = [
1418
// name chainable? enumerable? updateLength?
1519
"every", false, true, false,

src/model.js

+1-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
var Model = function(name, func) {
2-
// The model constructor.
3-
var model = function(attributes) {
4-
this.attributes = Model.Utils.extend({}, attributes)
5-
this.changes = {};
6-
this.errors = new Model.Errors(this);
7-
this.uid = [name, Model.UID.generate()].join("-")
8-
if (Model.Utils.isFunction(this.initialize)) this.initialize()
9-
this.emit("initialize", this)
10-
};
11-
12-
// Use module functionality to extend itself onto the constructor. Meta!
13-
Model.Module.extend.call(model, Model.Module)
2+
var model = Model.Model.extend()
143

154
model._name = name
16-
model.anyInstance = new Model.EventEmitter()
175
model.collection = new Model.Collection()
18-
model.persistence = Model.NullPersistence
19-
model.unique_key = "id"
20-
model
21-
.extend(Model.Callbacks)
22-
.extend(Model.ClassMethods)
23-
246
model.collection.listenTo(model.anyInstance)
257

26-
model.prototype = new Model.Model
27-
model.prototype.constructor = model
28-
298
if (Model.Utils.isFunction(func)) func.call(model, model, model.prototype)
309

3110
return model;

src/model_model.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
Model.Model = function() {}
1+
Model.Model = function(attributes) {
2+
this.attributes = Model.Utils.extend({}, attributes)
3+
this.changes = {}
4+
this.errors = new Model.Errors(this)
5+
this.uid = [name, Model.UID.generate()].join("-")
6+
if (Model.Utils.isFunction(this.initialize)) this.initialize()
7+
this.emit("initialize", this)
8+
}
9+
10+
Model.Model.extend = function() {
11+
var child = Model.Utils.inherits(this)
12+
child.anyInstance = new Model.EventEmitter()
13+
child.persistence = Model.NullPersistence
14+
child.unique_key = "id"
15+
Model.Utils.extend(child, Model.ClassMethods)
16+
return child
17+
}
218

319
Model.Model.prototype = {
420
attr: function(name, value) {

src/model_module.js

-11
This file was deleted.

src/model_utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ Model.Utils = {
2121
return -1
2222
},
2323

24+
inherits: function(parent) {
25+
var ctor = function() {}
26+
ctor.prototype = parent.prototype
27+
28+
var child = function() {
29+
parent.apply(this, arguments)
30+
}
31+
32+
child.prototype = new ctor()
33+
child.prototype.constructor = child
34+
35+
return child
36+
},
37+
2438
isFunction: function(obj) {
2539
return Object.prototype.toString.call(obj) === "[object Function]"
2640
}

test/index.html

-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
<script src="../src/model_model.js"></script>
2121
<script src="../src/model_local_storage.js"></script>
2222
<script src="../src/model_log.js"></script>
23-
<script src="../src/model_module.js"></script>
2423
<script src="../src/null_persistence.js"></script>
2524
<script src="../src/model_rest.js"></script>
2625
<script src="../src/model_uid.js"></script>
@@ -36,7 +35,6 @@
3635
<script src="tests/model_id_test.js"></script>
3736
<script src="tests/model_class_methods_test.js"></script>
3837
<script src="tests/model_errors_test.js"></script>
39-
<script src="tests/model_module_test.js"></script>
4038
<script src="tests/model_rest_test.js"></script>
4139
<script src="tests/model_uid_test.js"></script>
4240
<script src="tests/model_local_storage_test.js"></script>

test/tests/model_module_test.js

-27
This file was deleted.

0 commit comments

Comments
 (0)