3

単一の親ウィンドウ (基本的には変更された GWT) http://en.wikipedia.org/wiki/Google_Web_Toolkitでホストされる複数の iframe として実行される webapp があります。各 iframe がバックエンド サービスに個別にアクセスするのではなく、window.parent グローバル オブジェクトを使用してデータ サービスを共有しようとしています。このシングルトンパターンをセットアップします-

define(function(){
    var DocumentService = function () {

        //create namespace if not there
        if (!parent.OurApp) {
           parent.OurApp= {};
        }
        //grab the singleton instance and return it if there
        if (parent.OurApp.DocumentService) {

            return parent.OurApp.DocumentService;
        }
        //assign the global
        parent.OurApp.DocumentService= this;

        //start fetch on instantiation
        this.items;
        this.getItems();
        this.startInterval();

    } 
    DocumentService.prototype.getItems = function(){
      //Rest service call here
      //this.items.push(response); some pseudo-code here
    };
    DocumentService.prototype.startInterval = function(){
      var self = this;
      this.intervalId = parent.setInterval(function(){
         console.log("Fetching items @ " +new Date());
         this.getitems();
      },300000);
    };
  //return new instance or singleton instance
  return new DocumentService();
});

したがって、このプロセスは初期ロードで機能し、getItems() はサービスからデータをロードし、setInterval はループを開始して getItems を実行します。items 配列を見て、変更と追加を確認できます。

だから今、これはトリッキーになるところです。親ウィンドウではなく、右クリックの「フレームのリロード」でフレームを個別にリロードすると、サービスの親インスタンス化は引き続き実行されますが、iFrame が new DocumentService() を取得して「親」インスタンス シングルトンにアクセスすると、スレッドフレームがコンソールログを登録しなくなったり、アイテム配列に変更が加えられたりするため、同期などは行われなくなりました。なぜこれが起こるのかについての理由はありますか?シングルトン パターンに欠陥がありますか?

4

1 に答える 1

1

これを完全に解決することはできませんでしたが、別のルートに進みました。将来的には、postMessage などの組み込みのクロスフレーム通信メカニズムを使用します。

于 2014-04-28T03:00:51.410 に答える