Im building an app with Phonegap, Backbone.js and Require.js. The app implements Phonegap Push Notification. At the moment, the loading of the scripts in index.html looks like this:
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>
<script type="text/javascript" src="js/app/index.js"></script>
<script type="text/javascript">
app.initialize();
</script>
<script data-main="js/app" src="js/require.js"></script>
index.js looks like this:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
},
errorHandler:function(error) {
//alert('in errorHandler');
//alert(error);
},
/*
*
* For iOS
*/
tokenHandler:function(status) {
//save the status to server
},
onNotificationAPN: function(event) {
//display alert
},
};
In tokenHandler, I want to call a model I have defined as a Require.js module. So, I integrated index.js with Require.js. Index.html became this:
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="PushNotification.js"></script>
<script data-main="js/app" src="js/require.js"></script>
The index.js file now looks like this:
define(function (require) {
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
var pushNotification = window.plugins.pushNotification;
pushNotification.register(app.tokenHandler,app.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
},
errorHandler:function(error) {
//alert('in errorHandler');
//alert(error);
},
/*
*
* For iOS
*/
tokenHandler:function(status) {
//save the status to server
},
onNotificationAPN: function(event) {
//display alert
},
};
return app;
});
The in app.js, I do:
... ... ...
require(['jquery', 'backbone', 'app/router', 'app/index'], function ($, Backbone, Router, Index) {
var router = new Router();
Index.initialize();
Backbone.history.start();
});
The problem occurs in the callback to pushNotification.register(), which is app.onNotificationAPN. With the loading of the index.js as a Require module, this leads to an error:
processMessage failed: Error
When i user an anonymous function in place of the call to app.onNotificationAPN, I also get the same error.
What should the correct callback be?