gpt4 book ai didi

javascript - Ember CLI 异步初始化器和 cordova 就绪

转载 作者:行者123 更新时间:2023-11-29 17:01:29 28 4
gpt4 key购买 nike

使用 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接着是等待第一个适配器完成的第二个适配器。

最佳答案

注意:此答案已更新。检查下面的更新

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

您可以利用 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);
}
}
};

关于javascript - Ember CLI 异步初始化器和 cordova 就绪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356809/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com