简体   繁体   English

如何在Phonegap的onDeviceReady事件中添加Ember?

[英]How to add Ember to the onDeviceReady event in Phonegap?

In Phonegap we wait for the onDeviceReady event for our code to start executing. 在Phonegap中,我们等待onDeviceReady事件,以使我们的代码开始执行。 Following this path I added my Ember app like this: 按照此路径,我像这样添加了Ember应用程序:

var App = null; // Ember

var phonegap = {
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function () {
        App = Ember.Application.create({});
        // Now I can start adding my Ember stuff, but
        // even for a tutorial app that is somewhere between
        // 100 to 200 lines and it will be harder to maintain
        // inside here. So I wrap that with a function
        // and put it outside.
        addMyEmberStuff();
    }
}

function addMyEmberStuff() {

// Ember Routes, Controllers, Views etc.
App.Router.map(function() {

});

App.IndexController = Ember.Controller.extend({
    init: function () {
        this._super();
        // device API calls
        // create/render View(?)
        // trigger DOM events etc
    }
});

}

I know that I can initialize an Ember app outside of the onDeviceReady and everything will keep working. 我知道我可以在onDeviceReady之外初始化Ember应用程序,并且一切都会正常进行。 The problem is that the index page of the app has calls to the device API and also some DOM events must occur before Ember starts working its magic. 问题在于该应用程序的索引页已调用了设备API,并且在Ember开始发挥其魔力之前还必须发生一些DOM事件。

This seems to me the proper way of doing things. 在我看来,这是正确的处事方式。

How do I solve this design for the case of a larger app where I want to have each Ember Controller/View/Template in its own file? 对于较大的应用程序,我想如何将每个Ember Controller / View / Template放在自己的文件中,该如何解决此设计? I can't keep wrapping everything with the addMyEmberStuff function. 我不能一直用addMyEmberStuff函数包装所有内容。

You want prevent you Ember application from starting before PhoneGap is ready. 您希望在PhoneGap准备就绪之前阻止Ember应用程序启动。 To do this you can use defer and advance readiness. 为此,您可以使用延期和提前准备。

App = Ember.Application.create()
App.Post = ...
App.PostsRoute = ...
App.PostsController =  ...

App.deferReadiness();

document.addEventListener('deviceready', function() {
  App.advanceReadiness();
});

Once advanceReadiness is called Ember will begin routing for your application. 一旦advanceReadiness ,Ember将开始为您的应用程序路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM