检查mobilefirst是否已加载

时间:2016-09-28 19:27:06

标签: ibm-mobilefirst

我有角度应用程序并使用Mobile-first 8.如何检查MobileFirst是否已加载?我是否必须等待首先加载cordova?

在V7.1中,我有一个MobileFirst的包装器,它返回了cordova设备对象。但是,由于它现在是一个插件,我不知道如何检查......

我没有WLCommonInit功能。我正在使用“angular.run”函数,它在应用程序加载之前注入并执行。这是我在7.1中的代码

  (function () {
      'use strict';
  angular
    .module('local_worklight', [])
    .run(WorklightRun)

  WorklightRun.$inject = ['$rootScope', '$q'];

  function WorklightRun($rootScope, $q) {

    // Worklight statuss
    var initDeferred = $q.defer();
    $rootScope.worklightInitPromise = initDeferred.promise;
    $rootScope.worklightLoaded = false;

    var connectionDeferred = $q.defer();
    $rootScope.worklightConnectionPromise = connectionDeferred.promise;
    $rootScope.worklightConnected = false;

    // Listen for load / onload and start worklight init
    if (window.addEventListener) {
      window.addEventListener('load', loadHandler, false);
    } else if (window.attachEvent) {
      window.attachEvent('onload', loadHandler);
    }

    // Worklight connect options
    var wlConnectOptions = {
      timeout: 10000,

      onFailure: connectionFailure,

      onSuccess: function () {
        console.log('MFP: Connected');
        $rootScope.worklightConnected = true;
        connectionDeferred.resolve();
      }
    };

    // Worklight init options
    var wlInitOptions = {
      timeout: 10000,

      onConnectionFailure: initFailure,

      onFailure: initFailure,

      onSuccess: function () {
        console.log('MFP: Loaded');
        initDeferred.resolve();
        $rootScope.worklightLoaded = true;
        if (window.addEventListener) {
          window.addEventListener(WL.Events.WORKLIGHT_IS_CONNECTED, wlConnectedHandler, true);
          window.addEventListener(WL.Events.WORKLIGHT_IS_DISCONNECTED, wlDisconnectedHandler, true);
        } else if (window.attachEvent) {
          window.attachEvent(WL.Events.WORKLIGHT_IS_CONNECTED, wlConnectedHandler);
          window.attachEvent(WL.Events.WORKLIGHT_IS_DISCONNECTED, wlDisconnectedHandler);
        }
        console.log('MFP: About to connect - ' + JSON.stringify(wlConnectOptions, null, 2));
        WL.Client.connect(wlConnectOptions);
      }
    };

    var worklightLoadEvent = null;
    worklightLoadEvent = document.createEvent('CustomEvent');

    function loadHandler() {
      try {
        console.log('MFP: Load event detected, about to init - ' + JSON.stringify(wlInitOptions, null, 2));
        WL.Client.init(wlInitOptions);
      } catch (err) {
        console.error('MFP: "WL" init error - ' + err);
        initFailure(err);
        worklightLoadEvent.initCustomEvent('worklightLoadFailure', true, false, {});
        window.dispatchEvent(worklightLoadEvent);
      }
    }

    function wlConnectedHandler() {
      $rootScope.worklightConnected = true;
      WL.App.hideSplashScreen();
    }

    function wlDisconnectedHandler() {
      $rootScope.worklightFailureAcknowledged = true;
      $rootScope.worklightConnected = false;
      WL.App.hideSplashScreen();
    }

    function connectionFailure(err) {
      console.log('MFP: No Connection - ' + JSON.stringify(err, null, 2));
      connectionDeferred.reject(err);
    }

    function initFailure (err) {
      console.log('MFP: init failed - ' + err);
      initDeferred.reject(err);
      connectionFailure(err);
    }

  }
    })();

我使用Mobilefirst查看示例角应用程序,但它使用'angular.bootstrap'。但是,我需要将我的应用绑定到ng- *。所以,我不能用它

感谢您的帮助..

1 个答案:

答案 0 :(得分:0)

为什么不使用wlCommonInit?出于这个原因,它就在那里。

有关使用MobileFirst Foundation 8.0构建AngularJS应用程序的最佳实践,请参阅以下博客文章:https://mobilefirstplatform.ibmcloud.com/blog/2016/08/11/best-practices-for-building-angularjs-apps-with-mobilefirst-foundation-8.0/