1

I have the following code which registers my iOS device with my APNS server:

pushNotification.registerDevice({
    alert: true,
    badge: true,
    sound: true,
    pw_appid: "***",
    appname: "***"
},

function (status) {
    var deviceToken = status['deviceToken'];
},

function (status) {
    console.warn('failed to register : ' + JSON.stringify(status));
    navigator.notification.alert(JSON.stringify(['failed to register ', status]));
});

This runs onLoad, but I need to access deviceToken outside of the scope of pushNotification.registerDevice() function (status).

Is it possible, in this case, to access deviceToken which is inside a function within a function, outside of the function?

I thought I could make it a global variable, by using window.deviceToken, then calling that later on, but it returns undefined.

3
  • Are you it's not undefined because of the asynchronous nature of the registerDevice function? Commented Oct 24, 2012 at 23:15
  • You're right, I was calling it before it was defined. Commented Oct 24, 2012 at 23:16
  • Great, I posted an answer addressing that. Commented Oct 24, 2012 at 23:23

3 Answers 3

3

You could give deviceToken a broader scope:

var deviceToken;

pushNotification.registerDevice({
    alert: true,
    badge: true,
    sound: true,
    pw_appid: "***",
    appname: "***"
},

function(status) {
    deviceToken = status['deviceToken'];
},

function(status) {
    console.warn('failed to register : ' + JSON.stringify(status));
    navigator.notification.alert(JSON.stringify(['failed to register ', status]));
});​
Sign up to request clarification or add additional context in comments.

Comments

2

If you're assigning the token to a global and finding its undefined when you try to use it, you will probably find its because the registerDevice ajax hasn't yet received a response.

Try separating your callback into a separate function, and launch any dependant functions from there.

function registerSuccess(status) {
  // Store token globally
  token = status["deviceToken"];
  // or pass as an argument
  // call functions that depend on the token
  Foo(token);
  Bar(); // uses global
}

Personally I would try and pass as an argument, save polluting the global namespace.

Comments

2

Change it to make deviceToken a global variable so it is available in outside scopes by referencing the global scope directly.

function (status) {
    window.deviceToken = status['deviceToken'];
},

If you just want it in a parent scope, not necessarily at the global scope, you can define it in the desired parent scope and then NOT redeclare it in your function:

var deviceToken;

pushNotification.registerDevice({
    alert: true,
    badge: true,
    sound: true,
    pw_appid: "***",
    appname: "***"
},

function (status) {
    deviceToken = status['deviceToken'];
},

function (status) {
    console.warn('failed to register : ' + JSON.stringify(status));
    navigator.notification.alert(JSON.stringify(['failed to register ', status]));
});

6 Comments

Do I call the variable later by using window.deviceToken or deviceToken?
@Charlie - As long as no other scope has declared deviceToken, you can reference it with either window.deviceToken or deviceToken. window.deviceToken forces the global scope, but is not required for reading it's value if there is no other definition of deviceToken.
The global variable ended up working. The problem, as @Aesthete said, was that I was calling it before it wad defined.
@Charlie: however, you should put that variable just into a higher parent scope, putting it into the global scope as ultimate answer is both, bad karma and practice.
Why is it considered bad practice?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.