-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentTypeRegistry.js
More file actions
104 lines (95 loc) · 2.76 KB
/
ContentTypeRegistry.js
File metadata and controls
104 lines (95 loc) · 2.76 KB
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
(function (win, doc, $, _, Backbone, root) {
var BackboneModelProto = Backbone.Model.prototype,
ContentPrototype = Backbone.Model.extend({
name: null,
view: Backbone.View,
model: Backbone.Model,
setupContent: function (modelAttr, viewAttr) {
var model = new this.model(modelAttr);
(viewAttr || (viewAttr = {})).model = model;
this.set({
"view": new this.view(viewAttr),
"model": model
});
},
validate: function (attr) {
if (!(attr.model instanceof Backbone.Model)) {
return "model must inherit from Backbone.Model";
}
if (!(attr.view instanceof Backbone.View)) {
return "view must inherit from Backbone.View";
}
},
getView: function () {
return this.get("view");
},
getModel: function () {
return this.get("model");
},
destroy: function (options) {
BackboneModelProto.destroy.call(this.getModel(), options);
return BackboneModelProto.destroy.call(this, options);
},
exportHTML: function () {
return this.getView().exportHTML();
}
}),
ContenttypeRegistry = Backbone.Model.extend({
validateView: function (view, model) {
return !!(view.el && view.el.nodeName && view.model === model);
},
register: function (constructor) {
try {
var sampleContent = new constructor,
name = sampleContent.name,
// Test, if we can get the model
model = sampleContent.getModel(),
// and the view
view = sampleContent.getView(),
// Validate the view
isValidView = this.validateView(view, model),
// Check if everything gets destroyed, when we destroy the thing
nrOfDestroyTriggers = 0,
increse = function () {nrOfDestroyTriggers++;};
model.once("destroy", increse);
sampleContent.once("destroy", increse);
sampleContent.destroy();
// Check the results and set the constructor if everything's correct
if (isValidView && nrOfDestroyTriggers === 2 && _.isString(name)) {
return this.set(name, constructor);
}
} catch (e) {
return false;
}
},
isContenttypeAvailable: function (type) {
return type in this.attributes;
},
getContentPrototype: function (type) {
if (type) {
return this.get(type);
}
return ContentPrototype;
},
define: function (name, Model, View, initialize, register) {
if (typeof initialize === "boolean") {
register = initialize;
initialize = undefined;
}
var Contenttype = ContentPrototype.extend({
name: name,
view: View,
model: Model,
type: name,
initialize: initialize ? initialize : function (attr) {
this.setupContent(attr);
}
});
if (register) {
return this.register(Contenttype);
}
return Contenttype;
}
})
root.supply({"ContenttypeRegistry": new ContenttypeRegistry});
})(window, document, window.jQuery, window._, window.Backbone, window.ds);