【发布时间】:2017-12-06 22:03:07
【问题描述】:
我有一个使用 require.js 的主干应用程序。
在使用 require 我的 Backbone 路由器之前看起来像这样。
APP.views = {};
APP.Router = Backbone.Router.extend({
routes: {
'(/)' : 'index',
'about(/)' : 'about'
},
initialize : function(){
Backbone.history.start({ pushState: true });
},
index: function() {
this.showView( new APP.Views.IndexView() );
},
about: function() {
this.showView( new APP.Views.AboutView() );
},
showView : function( view ) {
if ( APP.views.current ) {
APP.views.current.remove();
}
APP.views.current = view;
$( '#page' ).html( view.render().$el );
}
});
我会将“当前”视图存储在全局变量中,并在每次更改路线并且生活良好时终止现有视图。
但是,我如何使用 require.js 来实现这一点?
我的 requirejs 路由器当前如下所示,但我不确定如何删除现有视图。虽然,我没有注意到任何典型的“僵尸视图”症状,但我觉得我应该删除现有视图。
define( function( require ){
// DEPS
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone');
// ROUTER
var Router = Backbone.Router.extend({
routes: {
'(/)' : 'index',
'about(/)' : 'about'
},
initialize : function(){
Backbone.history.start({ pushState: true });
},
index: function(){
this.showPage('index');
},
about: function() {
this.showPage('about');
},
showPage : function( pageName ) {
var view = 'views/pages/' + pageName;
require( [ view ] , function( Page ) {
var page = new Page();
$('#page').html( page.render().el );
});
}
});
return Router ;
});
【问题讨论】:
标签: javascript backbone.js requirejs iife