4

Ember.js でアプリケーションを作成しています。画像付きの問題が出題され、オーディオプレーヤーで次々と再生されるちょっとしたクイズです。質問が回答されると、次の質問をモデルとして questionRoute に遷移することで、次の質問が表示されます。

ユーザーがブラウザの戻るボタンを使用できないようにするために、質問ルートからのリダイレクト機能で質問が既に終了しているかどうかを確認します。その場合は、次の質問に戻します。

それは機能しますが、リダイレクトしたばかりの完成した質問に対して関数 setupController が引き続き実行されます。これにより、終了した質問がオーディオ プレーヤーで再度再生され、新しい質問が同時に再生されるという問題が発生します。

リダイレクトされた後、ルートがさらに setupController() と renderTemplate() を実行するのを防ぐ方法はありますか?

現在、独自の変数を使用して、ルートがリダイレクトされたかどうかを確認していますが、ハックな方法のように感じ、より良い方法があれば今すぐやりたいと思っています。

これは私のコードです:

//question route

Player.QuestionRoute = Em.Route.extend({
    redirect: function(question){

        Player.Quiz.setCurrentQuestion(question);

        nextQuestion = Player.Quiz.getNextQuestion();

        if(question.get('finished') == true && nextQuestion != null) {
            this.transitionTo('question', nextQuestion);
            //hackish way to prevent the other functions from executing
            this.set('hasBeenRedirected', true);
            return;
        }

        //hackish way to prevent the other functions from executing
        this.set('hasBeenRedirected', false);

    }, 
    setupController: function(controller, model){
        //hackish way to prevent the other functions from executing
        if(this.get('hasBeenRedirected') == true) return;
        controller.set('content', model);

        /* do some other things */

        controller.startQuestion();
    },
    renderTemplate: function(controller, model) {
        //hackish way to prevent the other functions from executing
        if(this.get('hasBeenRedirected') == true) return;

        this.render('stage'); 
        this.render('dock', {
            outlet: 'dock'
        }); 

        this.render(model.getPreferredTemplate(), {
            into: 'stage',
            outlet: 'question'
        });

        this.render("quizinfo", {
            into: 'stage',
            outlet: 'quizinfo',
            controller: this.controllerFor('quiz', Player.Quiz),
        });

    },
    model: function(param){
        return Player.Quiz.getQuestionAt(param.question_id);
    }

});
4

2 に答える 2

1

ember の最新バージョンは、 andフックredirectを支持して廃止されました。これにより、特に進む/戻るボタンの場合に、ユーザーがルートに入ったときに何が起こるかをよりきめ細かく制御できます。beforeModelafterModel

App.QuestionRoute = Ember.Route.extend({
  afterModel: function(question, transition) {
    nextQuestion = Player.Quiz.getNextQuestion();
    if (question.get('finished') == true && nextQuestion != null) {
      this.transitionTo('question', nextQuestion);
    }
  }
});

これらの新しいフックを使用するには、ember-latest で作業する必要があります。変更の詳細については、この要旨をご覧ください。

于 2013-06-23T15:47:31.773 に答える