2

I am using the PhoneGap LocalNotification plugin, which allows me to set local notifications at certain times.

The basic structure of the LocalNotification plugin is like this:

var notification = {
    init: function () {

    },
    clear_all: function () {
        notification.clear();
        plugins.localNotification.add({
            badge: 0,
        });

    },
    alert_ten: function () {
        var d = new Date();
        d = d.getTime() + 10 * 1000; //60 seconds from now
        d = new Date(d);
        plugins.localNotification.add({
            date: d,
            repeat: 'daily',
            message: varone + ' - ' + vartwo + '!',
            hasAction: true,
            badge: 1,
            id: '1',
            sound: 'horn.caf',
            background: 'app.background',
            foreground: 'app.running'
        });
    },
}

If you look at the message part of the notification, it consists of the following varone + ' - ' + vartwo + '!'. On page load, varone and vartwo are populated from a localStorage item. I then call notification.alert_ten() onLoad.

This all works fine, but there is one exception:

The localStorage items are set when the user interacts with a certain div, i.e., clicks it. Then, when the app is loaded, it checks to see these values, and alerts the message after 10 seconds, saying that this and that, which get their values from the LS.

If the user changes their mind, and interacts with a different div, which changes the LS items, the LocalNotification still runs with the original set of LS items.

This is to be expected, as JS will cache the variable within the function. A solution that I thought would work would be to define the variables globally, above var notification = {, and then when the user interacts with the div, update the vars to represent the new variables.

Global variables:

var varone = localStorage.getItem("favorite");
var vartwo = localStorage.getItem("favorite-status").substr(2).toLowerCase();
...

Updated variables:

...
var varone = text;
var vartwo = favstatus;
...

The function, notification.alert_ten() still runs with the original values defined within the global variable, instead of the updated ones.

2
  • Also, I'd stay away from the underscore _ naming convention. Would be more javascript syntax to name the functions clearAll() and alertTen(). Commented Oct 24, 2012 at 23:02
  • Yeah, I usually end up changing the names after I write the code. I don't know why, but I do. Commented Oct 24, 2012 at 23:06

1 Answer 1

1

You could write getter/setter functions. This is just a proof of concept, you can add whichever methods to this that you like. Just make sure to add this. before any property that you want to share between functions inside the object, or that you want accessible from outside the object.

var notification = {
  setX : function (newX) {
    this.x = newX;
  },
  getX : function () {
    return this.x;
  },
  outputX : function () {
    document.write('x is ' + this.x + '<br>');
  }
}

Initialize and use:

notification.setX(42);
console.log(notification.getX());

or

notification[x] = 42;
console.log(notification[x]);

or even

notification.x = 42;
console.log(notification.x);

DEMO

So your code could be something like (weeding out all but the interesting parts)

var notification = {
    getMessage: function() {
        return this.message;
    },
    setMessage: function() {
        this.message = localStorage.getItem("favorite") + ' - ' + 
                       localStorage.getItem("favorite-status").substr(2).toLowerCase() + '!';
    },
    alert_ten: function () {
        plugins.localNotification.add({
            message: this.getMessage()
        });
    }
}

// After some event I guess
notification.setMessage();
// After some other event?
notification.alert_ten();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.