@@ -30,14 +30,32 @@ define('cache', function (require, _exports, module) {
3030 var BaseView = cache . BaseView = Backbone . View . extend ( {
3131 initialize : function ( ) {
3232 var _this = this ;
33- if ( _this . templateUrl )
34- _this . __gotTemplate = cache . get ( _this . templateUrl )
35- . then ( function ( fn ) {
36- _this . templateFn = fn ;
37- } ) ;
33+ _this . subviews = { data : { } } ;
34+ _this . childViews = [ ] ;
35+ if ( _this . templateUrl ) {
36+ _this . __gotTemplate = cache . get ( _this . templateUrl )
37+ . then ( function ( fn ) {
38+ _this . templateFn = fn ;
39+ } ) ;
40+ }
3841 _ . bindAll ( _this , 'render' , 'postrender' ) ;
3942 _this . render ( ) ;
4043 } ,
44+ clear : function ( ) {
45+ var _this = this ;
46+ if ( _this . childViews . length ) {
47+ _this . childViews . forEach ( function ( view ) {
48+ view . remove ( ) ;
49+ } ) ;
50+ _this . childViews = [ ] ;
51+ }
52+ } ,
53+ remove : function ( ) {
54+ var _this = this ;
55+ _this . clear ( ) ;
56+ _this . undelegateEvents ( ) ;
57+ Backbone . View . prototype . remove . call ( _this ) ;
58+ } ,
4159 _render : function ( ) {
4260 this . $el . html ( this . templateFn ( ) ) ;
4361 } ,
@@ -55,6 +73,27 @@ define('cache', function (require, _exports, module) {
5573 _ . features . isHit ( node . dataset . feature ) || node . classList . add ( 'feature' ) ;
5674 } ) ;
5775 } ,
76+ loadSubview : function ( name , factory , selector ) {
77+ var _this = this ;
78+ var view ;
79+ var subviews = _this . subviews ;
80+ view = subviews . data [ name ] ;
81+ if ( ! view ) {
82+ view = factory ( ) ;
83+ }
84+ var current = subviews . current ;
85+ if ( name !== current ) {
86+ var currentView = subviews . data [ current ] ;
87+ if ( currentView ) {
88+ currentView . remove ( ) ;
89+ subviews . data [ current ] = null ;
90+ }
91+ }
92+ subviews . data [ subviews . current = name ] = view ;
93+ var $el = selector ? _this . $ ( selector ) : _this . $el ;
94+ $el . html ( view . render ( ) . el ) ;
95+ return view ;
96+ } ,
5897 getValue : function ( target ) {
5998 var key = target . dataset . id ;
6099 var value ;
@@ -79,6 +118,30 @@ define('cache', function (require, _exports, module) {
79118
80119 BaseView . prototype . postrender . call ( window ) ;
81120
121+ cache . BaseRouter = Backbone . Router . extend ( {
122+ initialize : function ( selector ) {
123+ var _this = this ;
124+ _this . views = { data : { } } ;
125+ _this . $root = $ ( selector ) ;
126+ } ,
127+ loadView : function ( name , factory ) {
128+ var _this = this ;
129+ var views = _this . views ;
130+ var view = views . data [ name ] ;
131+ if ( ! view ) view = views . data [ name ] = factory ( ) ;
132+ if ( name !== views . current ) {
133+ var currentView = views . data [ views . current ] ;
134+ if ( currentView ) {
135+ currentView . remove ( ) ;
136+ views . data [ views . current ] = null ;
137+ }
138+ views . data [ views . current = name ] = view ;
139+ }
140+ _this . $root . html ( view . el ) ;
141+ return view ;
142+ } ,
143+ } ) ;
144+
82145 ! function ( ) {
83146 var xhr = new XMLHttpRequest ;
84147 xhr . open ( 'GET' , '/icons/sprite.svg' , true ) ;
0 commit comments