@@ -30,6 +30,8 @@ define('cache', function (require, _exports, module) {
3030 var BaseView = cache . BaseView = Backbone . View . extend ( {
3131 initialize : function ( ) {
3232 var _this = this ;
33+ _this . subviews = { data : { } } ;
34+ _this . childViews = [ ] ;
3335 if ( _this . templateUrl ) {
3436 _this . __gotTemplate = cache . get ( _this . templateUrl )
3537 . then ( function ( fn ) {
@@ -39,6 +41,21 @@ define('cache', function (require, _exports, module) {
3941 _ . bindAll ( _this , 'render' , 'postrender' ) ;
4042 _this . render ( ) ;
4143 } ,
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+ } ,
4259 _render : function ( ) {
4360 this . $el . html ( this . templateFn ( ) ) ;
4461 } ,
@@ -54,6 +71,27 @@ define('cache', function (require, _exports, module) {
5471 node . innerHTML = _ . i18n ( node . dataset . i18n ) ;
5572 } ) ;
5673 } ,
74+ loadSubview : function ( name , factory , selector ) {
75+ var _this = this ;
76+ var view ;
77+ var subviews = _this . subviews ;
78+ view = subviews . data [ name ] ;
79+ if ( ! view ) {
80+ view = factory ( ) ;
81+ }
82+ var current = subviews . current ;
83+ if ( name !== current ) {
84+ var currentView = subviews . data [ current ] ;
85+ if ( currentView ) {
86+ currentView . remove ( ) ;
87+ subviews . data [ current ] = null ;
88+ }
89+ }
90+ subviews . data [ subviews . current = name ] = view ;
91+ var $el = selector ? _this . $ ( selector ) : _this . $el ;
92+ $el . html ( view . render ( ) . el ) ;
93+ return view ;
94+ } ,
5795 getValue : function ( target ) {
5896 var key = target . dataset . id ;
5997 var value ;
@@ -78,6 +116,30 @@ define('cache', function (require, _exports, module) {
78116
79117 BaseView . prototype . postrender . call ( window ) ;
80118
119+ cache . BaseRouter = Backbone . Router . extend ( {
120+ initialize : function ( selector ) {
121+ var _this = this ;
122+ _this . views = { data : { } } ;
123+ _this . $root = $ ( selector ) ;
124+ } ,
125+ loadView : function ( name , factory ) {
126+ var _this = this ;
127+ var views = _this . views ;
128+ var view = views . data [ name ] ;
129+ if ( ! view ) view = views . data [ name ] = factory ( ) ;
130+ if ( name !== views . current ) {
131+ var currentView = views . data [ views . current ] ;
132+ if ( currentView ) {
133+ currentView . remove ( ) ;
134+ views . data [ views . current ] = null ;
135+ }
136+ views . data [ views . current = name ] = view ;
137+ }
138+ _this . $root . html ( view . el ) ;
139+ return view ;
140+ } ,
141+ } ) ;
142+
81143 ! function ( ) {
82144 var xhr = new XMLHttpRequest ;
83145 xhr . open ( 'GET' , '/images/sprite.svg' , true ) ;
0 commit comments