Ember CLI异步初始化器&科尔多瓦准备就绪

时间:2014-12-08 11:17:42

标签: javascript cordova ember.js ember-cli

使用ember cli v0.1.4,ember 1.8.1& cordova 3.7,我正在利用初始化器等待设备准备好;

var CordovaInitializer = {
  name: 'waitForCordova',
  before:'i18n',

  initialize: function(Container, App) {

    if(window.cordova) {
      console.log('defering app launch until cordova is ready...');
      App.deferReadiness();

      document.addEventListener('deviceready', function(){
        console.log('lift off!');

        App.advanceReadiness();
      }, false);
    }
  }
};

export default CordovaInitializer;

我的第二个初始化者也需要等待cordova应用程序准备就绪 -

export default {
  name: 'i18n',
  after:'waitForCordova',

  initialize: function(Container, App) {

     console.log('initializing translations'); 

     /* snip */
  } 

};

我期待以下输出 -

$ defering app launch until cordova is ready...
$ lift off!
$ initializing translations

但是,我得到以下内容 -

$ defering app launch until cordova is ready...
$ initializing translations
$ lift off!

如何让我的第二个初始化程序等到cordova加载?他们以正确的顺序开火,但我期待第二个等待App.advanceReadiness

基本上我需要一个如此处所述的异步初始化器 - ember initializers,然后是第二个等待第一个完成的适配器。

1 个答案:

答案 0 :(得分:5)

注意:此答案已更新。请查看以下更新

========================旧答案================== =======

您可以利用Ember.Instrumentation同步初始值定位器

// app/initializers/cordova.js

import Ember from 'ember';

var CordovaInitializer = {
  name: 'waitForCordova',
  before: 'i18n',
  initialize: function(container, application) {
    application.set('shouldSubscribeCordovaDeviceReady', false);
    if (window.cordova) {
      console.log('defering app launch until cordova is ready...');

      application.set('shouldSubscribeCordovaDeviceReady', true);
      application.deferReadiness();

      document.addEventListener('deviceready', function() {
        console.log('lift off!');
        Ember.Instrumentation.instrument('cordova:deviceready', null, Ember.K);
        application.set('shouldSubscribeCordovaDeviceReady', false);
        application.advanceReadiness();
      }, false);
    }
  }
};

export
default CordovaInitializer;
// app/initializers/i18n.js

import Ember from 'ember';

var initializeI18n = function(container, application) {
  console.log('initializing translations');
  //do your thing
};

export
default {
  name: 'i18n',
  after: 'waitForCordova',
  initialize: function(container, application) {
    var subscriber;
    if (application.get('shouldSubscribeCordovaDeviceReady')) {
      application.deferReadiness();
      subscriber = Ember.Instrumentation.subscribe('cordova:deviceready', {
        before: Ember.K,
        after: function() {
          initializeI18n(container, app);
          application.advanceReadiness();
          Ember.Instrumentation.unsubscribe(subscriber);
        }
      });
    } else {
      initializeI18n(container, application);
    }
  }
};

========================旧答案结束=================== =

===================更新后的答案===================

为了同步初始值设定项,您可以使用Ember.Evented扩展Ember.Application类。通过这种方式,application实例将具有pubsub个功能。

// app/app.js

import Ember from 'ember';

// ...

var App;

App = Ember.Application.extend(Ember.Evented, {

  // ...  

});

// ...

export default App;
// app/initializers/cordova.js

import Ember from 'ember';

var CordovaInitializer = {
  name: 'waitForCordova',
  before: 'i18n',
  initialize: function(registry, application) {
    application.set('shouldSubscribeCordovaDeviceReady', false);
    if (window.cordova) {
      console.log('defering app launch until cordova is ready...');

      application.set('shouldSubscribeCordovaDeviceReady', true);
      application.deferReadiness();

      document.addEventListener('deviceready', function() {
        console.log('lift off!');
        application.trigger('cordova:deviceready');
        application.set('shouldSubscribeCordovaDeviceReady', false);
        application.advanceReadiness();
      }, false);
    }
  }
};

export
default CordovaInitializer;
// app/initializers/i18n.js

import Ember from 'ember';

var initializeI18n = function(container, application) {
  console.log('initializing translations');
  //do your thing
};

export
default {
  name: 'i18n',
  after: 'waitForCordova',
  initialize: function(registry, application) {
    if (application.get('shouldSubscribeCordovaDeviceReady')) {
      application.deferReadiness();
      application.one('cordova:deviceready', null, function() {
        initializeI18n(container, app);
        application.advanceReadiness();
      });
    } else {
      initializeI18n(container, application);
    }
  }
};