私は最近同じ問題に遭遇し、マリオネットのイベントシステムを使用して、show() を起動する前にビューがステータスを通信できるようにするパターンに落ち着きました。また、複雑さが増すrequireJSも使用していますが、このパターンが役立ちます!
クリックするとイベントを発生させて新しいビューをロードする UI 要素を持つ 1 つのビューを作成します。これはサブビューまたはナビビューである可能性があります-問題ではありません。
App.execute('loadmodule:start', { module: 'home', transition: 'left', someOption: 'foo' });
そのイベントは Wreqr の「setHandlers」によってキャッチされ、ビューの読み込みシーケンスをトリガーします (私の場合、状態遷移を処理するために一連のロジックを実行しています)。「開始」シーケンスは、新しいビューを開始し、必要なオプションを渡します。
var self = this;
App.commands.setHandlers({'loadmodule:start': function( options ) { self.start( options );
次に、読み込み中のビューがコレクション/モデルを処理し、初期化時にフェッチします。ビューはモデル/コレクションの変更をリッスンし、wreqr の executre コマンドを使用して新しい「準備完了」イベントを発生させます。
this.listenTo( this.model, 'change', function(){
App.execute('loadmodule:ready', { view: this, options: this.options });
});
その準備完了イベント (およびビュー opject 参照を含むオプション) をキャッチし、show() をトリガーする別のハンドラーがあります。
ready: function( options ) {
// catch a 'loadmodule:ready' event before proceeding with the render / transition - add loader here as well
var self = this;
var module = options.options.module;
var view = options.view;
var transition = options.options.transition;
var type = options.options.type;
if (type === 'replace') {
self.pushView(view, module, transition, true);
} else if (type === 'add') {
self.pushView(view, module, transition);
} else if (type === 'reveal') {
self.pushView(view, module, transition, true);
}
},
pushView: function(view, module, transition, clearStack) {
var currentView = _.last(this.subViews);
var nextView = App.main.currentView[module];
var self = this;
var windowheight = $(window).height()+'px';
var windowwidth = $(window).width()+'px';
var speed = 400;
switch(transition) {
case 'left':
nextView.show( view );
nextView.$el.css({width:windowwidth,left:windowwidth});
currentView.$el.css({position:'absolute',width:windowwidth,left:'0px'});
nextView.$el.animate({translate: '-'+windowwidth+',0px'}, speed, RF.Easing.quickO);
currentView.$el.animate({translate: '-'+windowwidth+',0px'}, speed, RF.Easing.quickO, function(){
if (clearStack) {
_.each(_.initial(self.subViews), function( view ){
view.close();
});
self.subViews.length = 0;
self.subViews.push(nextView);
}
});
break;
システム全体のまともな要点を書き上げて、来週かそこらでここに投稿しようと思います。