forked from orizens/echoes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.js
137 lines (118 loc) · 2.9 KB
/
temp.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
(function(){
// check for Backbone
// check for Underscore
var _ = this._;
var Backbone = this.Backbone;
// if Underscore or Backbone have not been loaded
// exit to prevent js errors
if (!_ || !Backbone || !JSON) {
return;
}
// * check for requirejs?
// save reference to Backbone.View's contructore to allow
// overiding
var ViewInitialize = Backbone.View.prototype.constructor;
var ViewExtend = Backbone.View.extend;
_.extend(Backbone.View.prototype, {
_init: function() {
var key;
// activate the switcher's configuration
if (this.switcher) {
key = this.switcher.key || "";
if (this.switcher.options) {
this.parseOptions();
}
this.listenTo(this.model, 'change:' + key, this.handleResource);
}
this._userInit.apply(this, arguments);
},
parseOptions: function() {
var options = this.switcher.options;
// set the target to append the views to
if (options.target) {
this.$target = this.$(options.target);
} else {
this.$target = this.$el;
}
this._views = this.switcher.views;
},
handleResource: function(model, resource) {
if (this._currentView) {
this._currentView.remove();
}
this.currentResource = resource;
// render the view object
this._render();
},
// TODO: apply transitions
_render: function () {
this._currentView = new this._views[this.currentResource]({ model: this.model });
this.$target.append(this._currentView.render().el);
// to allow transitions between views
// this.tid = setTimeout(_.bind(function(){
// this._currentView.showViews();
// clearTimeout(this.tid);
// }, this), 0);
this.trigger("after:render");
}
});
_.extend(Backbone.View, {
extend: function (options) {
// keep user's initialize
options._userInit = options.initialize;
// replace user's initialize with the plugin's init
options.initialize = Backbone.View.prototype._init;
return ViewExtend.call(this, options);
}
});
}());
var views = {
'a': {
view: 'TocView/TocView',
model: ['course' ],
collection: ['classesCollection', 'studyClassStates'],
router: {
routes: {
'course/:courseId/toc/studyClass/:studyClassId': 'showTableOfContents'
},
handlers: {
showTableOfContents: function (courseId, studyClassId) {//
console.log('course');
}
}
}
},
'b': {
view: 'TocItemView/TocItemView',
model: ['course'],
router: {
routes: {
'course/:courseId/studyClass/:studyClassId/item/:itemId': 'showTocItem'
},
handlers: {
showTocItem: function (courseId, studyClassId, itemId) {
}
}
}
}
};
var layout = Backbone.View.extend({
switcher: {
key: 'resource',
views: {
viewA: SomeViewA,
viewB: SomeViewB
}
},
initialize: function() {
console.log('init...', this.options);
}
});
var layoutModel = Backbone.Model.extend({
defaults: {
'resource': 'a'
}
});
var layView = new layout({
model: new layoutModel()
});