Sencha触摸听取phonegap事件

时间:2013-06-11 13:46:25

标签: events extjs sencha-touch cordova

我正试图让Sencha Touch听取phonegap发布的事件,我在网上找到的任何帮助都只指向sencha事件。我也尝试过设置听众和“.on”,但它似乎不适用于外部事件。

我不是在谈论任何特定事件,只是像“简历”或“batterystatus”这样的事件。

请不要回复提及Ext.device,因为Sencha对Ext.device的支持是有限的和过时的,因为它总是在一家公司试图提供一个不同公司的包装时,我想采取充分利用最新的phonegap版本提供的所有功能。

2 个答案:

答案 0 :(得分:2)

您应该能够像这样添加特定于PhoneGap的侦听器:

document.addEventListener("deviceready", function() {
    document.addEventListener("backbutton", MyApp.backButtonListener, false);
    document.addEventListener("menubutton", MyApp.menuButtonListener, false);
}, false);

MyApp.backButtonListener = function(e) {
    e.preventDefault();
    //do other stuff here...
}

MyApp.menuButtonListener = function(e) {
    e.preventDefault();
    //do other stuff here...
}

答案 1 :(得分:2)

你可以做这样的事情,我已经测试了它并且它正在工作!

Ext.application({
    name: 'TestApp',   
    ...
    launch: function() {
        // Hook up events of PhoneGap here
        document.addEventListener("pause", this.onPause, false);
        document.addEventListener("resume", this.onResume, false);

        // Destroy the #appLoadingIndicator element
        Ext.fly('appLoadingIndicator').destroy();

        // Initialize the main view
        Ext.Viewport.add(Ext.create('TestApp.view.Main'));
    },
    onPause: function() {
        // this code is fine but using 'this' here is not safe 
        console.log("onPause");
    },
    onResume: function() {
        console.log("onResume");
    }
    ...
});
编辑


事实证明将处理程序放在app.js文件中并不是一个好主意(虽然它仍然可以工作)。因为如果你升级sencha,它希望这个文件不被修改太多。

所以你应该把它放在主控制器的启动功能中。控制器的启动功能在应用程序的启动功能之后执行。

Ext.define('TestApp.controller.Main', {
    extend: 'Ext.app.Controller',
    ...
    launch: function() {
        // Hook up events of PhoneGap here
        document.addEventListener("pause", this.onPause, false);
        document.addEventListener("resume", this.onResume, false);
    },
    onPause: function() {
        // this code is fine but using 'this' here is not safe 
        console.log("onPause");
    },
    onResume: function() {
        console.log("onResume");
    }
    ...
});