2014-02-19 61 views
0

我需要基本回調函數的幫助。我有一個主要的index.html文件引用script.js。在index.html裏面,它只是調用app.initialize();從script.js初始化函數。下面是下面的script.js代碼:被基本回調函數困惑

var app = { 
    // Application Constructor 
    initialize: function() { 
     this.bindEvents(); 
    }, 
    // Bind Event Listeners 
    // 
    // Bind any events that are required on startup. Common events are: 
    // 'load', 'deviceready', 'offline', and 'online'. 
    bindEvents: function() { 
     document.addEventListener('deviceready', this.onDeviceReady, false); 
    }, 
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicity call 'app.receivedEvent(...);' 
    onDeviceReady: function() { 
     app.receivedEvent('deviceready'); 
    }, 
    tokenHandler:function(msg) { 
     console.log("Token Handler " + msg); 
     alert(msg); 
    }, 
    errorHandler:function(error) { 
     console.log("Error Handler " + error); 
     alert(error); 
    }, 
    // result contains any message sent from the plugin call 
    successHandler: function(result) { 
     alert('Success! Result = '+result) 
    }, 
    // Update DOM on a Received Event 
    receivedEvent: function(id) { 
     var pushNotification = window.plugins.pushNotification; 
     // TODO: Enter your own GCM Sender ID in the register call for Android 
     if (device.platform == 'android' || device.platform == 'Android') { 
      pushNotification.register(this.successHandler, this.errorHandler,{"senderID":"SENDER ID HERE","ecb":"app.onNotificationGCM"}); 
     } 
     else { 
      pushNotification.register(this.tokenHandler,this.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"}); 
     } 
     var parentElement = document.getElementById(id); 
     var listeningElement = parentElement.querySelector('.listening'); 
     var receivedElement = parentElement.querySelector('.received'); 

     listeningElement.setAttribute('style', 'display:none;'); 
     receivedElement.setAttribute('style', 'display:block;'); 

     console.log('Received Event: ' + id); 
    }, 
    // iOS 
    onNotificationAPN: function(event) { 
     var pushNotification = window.plugins.pushNotification; 
     console.log("Received a notification! " + event.alert); 
     console.log("event sound " + event.sound); 
     console.log("event badge " + event.badge); 
     console.log("event " + event); 
     if (event.alert) { 
      navigator.notification.alert(event.alert); 
     } 
     if (event.badge) { 
      console.log("Set badge on " + pushNotification); 
      pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge); 
     } 
     if (event.sound) { 
      var snd = new Media(event.sound); 
      snd.play(); 
     } 
    }, 
    // Android 
    onNotificationGCM: function(e) { 
     switch(e.event) 
     { 
      case 'registered': 
       if (e.regid.length > 0) 
       { 
        // Your GCM push server needs to know the regID before it can push to this device 
        // here is where you might want to send it the regID for later use. 
        alert('registration id = '+e.regid); 

        // Your GCM push server needs to know the regID before it can push to this device 
        // here is where you might want to send it the regID for later use. 
        PushWoosh.appCode = "CODE GOES HERE"; 
        PushWoosh.register(e.regid, function(data) { 
         alert("PushWoosh register success: " + JSON.stringify(data)); 
        }, function(errorregistration) { 
         alert("Couldn't register with PushWoosh" + errorregistration); 
        }); 
       } 
      break; 

      case 'message': 
       // if this flag is set, this notification happened while we were in the foreground. 
       if (e.foreground) 
       { 
        alert(e.payload.message); 
        alert(e.payload.pagenotification); 

       } 
       else 
       { // otherwise we were launched because the user touched a notification in the notification tray. 
        if (e.coldstart) 
        { 
         alert(e.payload.message); 
         alert(e.payload.pagenotification); 
        } 
        else 
        { 
         alert(e.payload.message); 
         alert(e.payload.pagenotification); 
        } 
       } 
      break; 

      case 'error': 
       alert('GCM error = '+e.msg); 
      break; 

      default: 
       alert('An unknown GCM event has occurred'); 
       break; 
     } 
    } 
}; 

內onNotificationGCM的,對在任的前景/冷啓動情況「消息」的情況下,我想要重新回到e.payload.pagenotification主索引調用app.initialize()的.html頁面。有沒有什麼方法可以解釋我如何將這些信息返回到索引頁面。

任何幫助將不勝感激,謝謝! - 全天候

回答

0

有一個全局變量在您的index.html和更新從前臺/冷啓動代碼塊這個變量,這樣你將有變量包含您可以在使用index.html信息

0

因此,事件基本上是在事件發出時將被調用的監聽器(回調函數)列表。

您可以創建自己的應用程序事件。 有了這個功能,您可以從index page中收聽這些應用事件。

我會盡力幫助您根據我瞭解您的問題,使用不同的示例。

的index.html來源的script.js

function myApp() { 
    this.initialize(); 
} 

myApp.prototype = { 

    initialize: function() { 
     this.event = document.createElement('div'); 
     // do your stuff here 
    }, 

    emit: function (eventName,data) { 
     var event = new CustomEvent(eventName, { detail: data }); 
     this.event.dispatchEvent(event) 
    }, 

    onNotificationGCM: function (e) { 
     // do your stuff here 
     this.events.notificationGCM.emit('notificationGCM',e); 
    } 

}; 

var app = new myApp; 

在上面的例子中的

<html> 
<script type='text/html' src='script.js'></script> 
<script type='text/html'> 
    app.event.addEventListener('notificationGCM', function (e) { 
     // you can access the event data 
     console.log(e.detail.x); 
    }); 
    app.emit('notificationGCM',{ x: 1 }); 
</script> 
</html> 

來源,我創建了一個notificationGCM事件,但您可以創建任何事件你想,發射他們,只要你喜歡,並從index page聽他們。