0

I am facing a problem while fetching the value from HTML5 localstorage. When i am setting value to the key using JSON.stringify the callback is getting removed. I am stuck with this problem. Below is the code snippet.

var topics = {};
var subscribe = function (topic, fn) 
{  
if (!topics[topic]) 
{         
topics[topic] = [];}
topics[topic].push({ callback: fn,context: this  });
window.localStorage.setItem('myArray', JSON.stringify(topics[topic]));
return this;
};
var publish = function (topic) {
var args;args = Array.prototype.slice.call(arguments, 1);
var myArray = JSON.parse(window.localStorage.getItem('myArray'));
for (var i = 0, l = myArray.length; i < l; i++) {var subscription = myArray[i];
        subscription.callback.apply(subscription.context, args);
    }
    return this;

};

Any help will be highly appreciated.

Cheers, Sajesh Nambiar

1
  • A JavaScript function does not have a corresponding value in JSON. Commented Sep 18, 2012 at 5:36

2 Answers 2

2

There is currently no JSON implementation for storing a function.

That is why the callback is being removed.

Sign up to request clarification or add additional context in comments.

4 Comments

Possibly, but I question whether this is the appropriate route to take here. What exactly are you trying to do?
i am designing a pub sub pattern in JS...for that i am storing the published action in the localstorage and then retrieving the action for the subscribers from the localstorage
I would then suggest storing an ID of some sort instead and then call your action with the ID on load from localstorage.
k Adam so JSON.stringify doesn't have support to function calls..if i remove JSON.stringify and simply use window.localStorage.getItem('myArray') i should get callback function but in subscription.callback i am receiving an exception
0

As Adam correctly explained, JSON is for representing pure data and not code logic. This is a matter of separation of concerns.

The "right" way would probably be to retrieve the data from the local storage, wrap it in your custom JavaScript object which then has already functionalities attached for operating on the data. I'm thinking about something like

var MyDataObj = function(){
    //constructor
    //load the data from the local storage and associate it to this object like
    this.data = JSON.parse(window.localStorage.getItem('myArray'));
};

MyDataObj.prototype.executeAction = function(){
   //peform your operations like invoking the subscriptions
};

The usage would then be

var dataObj = new MyDataObj();
dataObj.executeAction();

I'm not quite sure what's the intention behind your invocation of the callback methods, in my opinion your object encapsulate what to invoke and how.

Hope it helps

1 Comment

my scenario is Mediator.js will have publish and subscribe methods as described in my code snippet..now 1.htm broadcast an event which other html has to capture and do the necessary action as per the business logic so that is why the callback is required...i dont know whether i am making it bit complex

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.