0

前景ページと背景ページで構成される拡張機能があります。背景は永続的で、いくつかのモデルが含まれています。前景は短命で、ビューが含まれています。そのため、フォアグラウンドのビューは、永続的なバックグラウンド モデルから読み取ることによって状態を維持します。

拡張機能の内部では、背景のウィンドウへの参照を提供するchrome.extension.getBackgroundPage()を介して背景ページと対話できます。

私は RequireJS を使用して JavaScript をモジュール化しています。そのため、モデルの宣言は次のようになります。

//  Exposed globally so that Chrome Extension's foreground can access through chrome.extension.getBackgroundPage()
var VideoDisplayButton = null;

define(function () {
    'use strict';

    var videoDisplayButtonModel = Backbone.Model.extend({

        defaults: {
            enabled: false
        },

        toggleEnabled: function () {
            this.set('enabled', !this.get('enabled'));
        }

    });

    VideoDisplayButton = new videoDisplayButtonModel;

    return VideoDisplayButton;
});

およびそのビューのカウンターパート:

this.videoDisplayButtonView = new VideoDisplayButtonView({
    model: chrome.extension.getBackgroundPage().VideoDisplayButton
});

バックグラウンド ページでホストされているモデルのインスタンスをフォアグラウンドで正常に参照できるようにするために、AMD モジュールを壊さなければなりません。

理想的には、バックグラウンドのモデル宣言を次のようにしたいと考えています。

define(function () {
    'use strict';

    var VideoDisplayButton = Backbone.Model.extend({

        defaults: {
            enabled: false
        },

        toggleEnabled: function () {
            this.set('enabled', !this.get('enabled'));
        }

    });

    return VideoDisplayButton;
});

これは、Chrome 拡張機能内で AMD モジュールを操作する際の厳しい制限ですか? または、何か不足していますか?

4

1 に答える 1