-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lao.js
109 lines (94 loc) · 2.84 KB
/
Lao.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
(function (win, doc, $, _, Backbone) {
var LaoGod = new (Backbone.Collection.extend ({
initialize: function () {
this.on ("add", this.setView, this);
},
setView: function (m) {
m.view = new LaoView({
model: m,
el: m.get("el")
})
}
}))
var toJSON = function () {
var o = _.clone(this.attributes), i;
for (i in o) {
!o[i] || !_.isFunction(o[i].toJSON) || (o[i] = o[i].toJSON());
}
return o;
};
var LaoModule = {}, LaoViewModule = {}, privateModules = {}, nameSpaceSplitter = /^(.+)\./, nameSpaces = {};
var Lao = Backbone.Model.extend({
initialize: function (attr) {
LaoGod.add(this);
},
create: function (name, data) {
if (LaoModule[name]) {
if (this.get("module")) {
this.get("module").destroy();
}
this.set("module", new (Lao.get(name))(data), {"name": name});
}
},
toJSON: toJSON
});
Lao.Model = Backbone.Model.extend({
toJSON: toJSON
});
Lao.Collection = Backbone.Collection;
// Static functions
_.extend(Lao, {
define: function (name, inheritsFrom, protoModel, protoView) {
var namespace;
if (name && _.isString(name) && inheritsFrom && _.isFunction(inheritsFrom.extend)) {
LaoModule[name] = inheritsFrom.extend(protoModel);
LaoViewModule[name] = _.extend({}, this.getView(name) || {}, protoView);
if ((namespace = nameSpaceSplitter.exec(name))) {
nameSpaces[namespace.pop()] = true;
}
}
return Lao;
},
isNameSpaceDefined: function (namespace) {
return !!nameSpaces[namespace];
},
get: function (name) {
return LaoModule[name];
},
getView: function (name) {
return LaoViewModule[name];
}
})
var LaoViewProto = {
initialize: function () {
this.model.on("change:module", this._resetLaoView, this);
this.model.on("change:el", this._setElement, this);
},
_formerModel: {},
_setElement: function (m, el) {
this.setElement(el);
},
_resetLaoView: function (formerModel, newModel, options) {
var name = options.name,
newViewProto = Backbone.View.extend(Lao.getView(name));
// Unbound any Listeners and call the reset-function if there is any
this.undelegateEvents();
if (_.isFunction(this.reset)) this.reset();
// remove all previously set functions
_.each(this, function (v, i) {if (!forbiddenKeys.test(i)) delete this[i]}, this);
this._formerModel = formerModel;
// Now create a new View-instance and bind all its properties to this view
_.each(new newViewProto({
model: newModel,
el: newModel.get("el") || formerModel.get("el")
}), function (v, i) {
if (!forbiddenKeys.test(i)) {
this[i] = _.isFunction(v) ? _.bind(v, this) : v;
}
}, this);
}
},
forbiddenKeys = new RegExp("^(" + _.keys(LaoViewProto).join("|") + ")$"),
LaoView = Backbone.View.extend (LaoViewProto);
Backbone.Lao = Lao;
})(window, window.document, window.jQuery, window._, window.Backbone)