0

親ビューと子ビューの両方のイベントを適切に登録して発火させる正しい方法は何ですか?

このアプローチでは、親のイベントが子供のイベントを一掃します。また、子のイベントを の一部として親に渡し、登録する前に親にそれらを拡張させようとしましたoptionsが、親のイベントは機能しなくなりました。

// this is helpers/authorization/views/authHelper
export class AuthView extends Backbone.View {
    constructor(options?) {
        this.events = {
            'keypress #auth': 'setAuthorizationCodeKeypress',
            'click .create': 'setAuthorizationCode'
        };
        super(options);
    }
}

import AV = module("helpers/authorization/views/authHelper")
export class PageHelperView extends AV.AuthView {
    constructor(options?) {
        this.events = {
            'click .configHead': 'toggle'
        }
        super(options);
    }
}

それらが同じ要素を共有し、new EHV.EncoderAPIHelperView().render();それらをレンダリングするための呼び出しのみを必要とすることを望みます。

4

2 に答える 2

1

注:おそらくより良い答えで編集

親イベントをオブジェクト内で直接宣言できます。これにより、新しいコンストラクターを作成する必要がなくなります。親ビューは次のようになります。

export class AuthView extends Backbone.View {
    events = {
        'keypress #auth': 'setAuthorizationCodeKeypress',
        'click .create': 'setAuthorizationCode'        
    }
}

これで、子を次のように書き換えることができます。

import AV = module("helpers/authorization/views/authHelper")
export class PageHelperView extends AV.AuthView {
    initialize(options?) {
        this.events = {
            'click .configHead': 'toggle'
        }        
    }
}

_.extend呼び出しは、不足しているエントリをイベントに追加し、キーを共有するエントリを置き換えます。(詳細はこちら

また、私は typescript があまり得意ではないので、このコードには 1 つまたは 2 つの問題があるかもしれません。

于 2013-06-06T14:13:38.040 に答える
0

完全な実用的なソリューション:

親ビュー:

export class AuthView extends Backbone.View {

    constructor(options?) {
        this.events = {
            'keypress #auth': 'setAuthorizationCodeKeypress',
            'click .create': 'setAuthorizationCode'
        };

        super(options);
    }
}

子ビュー:

import AV = module("helpers/authorization/views/authHelper")
export class PageHelperView extends AV.AuthView {

    constructor(options?) {
        super(options);
    }

    initialize(options) {
        this.events = _.extend(this.events, {
            'click .configHead': 'toggle'
        });
    }
}
于 2013-06-07T13:33:37.747 に答える