0

render()ビュー全体、または HTML フォームで表されるビューのごく一部をレンダリングできるようにするために、ビューのメソッドを 2 つの別々のメソッドに分割しました。2 つのrender()方法は次のようになります。

    render: function () {
        var templateData = _.extend({}, this.model.attributes, localizedText),
            compiledLoadStopTemplate = Handlebars.compileClean(template);

        this.isClosed = false;

        this.triggerMethod("before:render", this);
        this.triggerMethod("item:before:render", this);

        this.$el.html(compiledLoadStopTemplate(JSON.parse(JSON.stringify(templateData))));
        this.renderAppointmentForm();

        this.bindUIElements();

        this.showStatusNotification();

        this.triggerMethod("render", this);
        this.triggerMethod("item:rendered", this);

        return this;
    },
    renderAppointmentForm: function () {
        var templateData = _.extend({}, this.model.attributes, localizedText, { notification: this.notification }),
            compiledAppointmentFormTemplate = Handlebars.compileClean(appointmentFormTemplate);

        this.triggerMethod("before:render", this);
        this.triggerMethod("item:before:render", this);

        this.$el.find(".hook-appointment-form-container").html(compiledAppointmentFormTemplate(JSON.parse(JSON.stringify(templateData))));

        this.bindUIElements();

        this.showStatusNotification();

        this.triggerMethod("render", this);
        this.triggerMethod("item:rendered", this);

        return this;
    },

さて、ここには明らかに大量の重複コードがあります。テンプレート データ、テンプレート、および実際のhtml()呼び出しは一意ですが、他のほとんどすべての行はそれらの間で共通です。

テンプレートデータ、コンパイルされたテンプレート、およびhtml()行を提供し、他の発火前/発火後メソッドの残りを自動的に普遍的に配置できるようにするラップされたメソッドがあればいいのですが、私は考案できませんでした.wrap()実際に機能したアンダースコアを使用した方法。

このニーズに完全に適合する、より高度なプログラミングの概念があると確信していますが、今は把握できていません。

4

2 に答える 2