it'll be undefined because the alert('outside '...)
is being called before the requestValue
callback. It's asyncronous so the alert will not wait for requestValue to finish before it executes.
You could either call a function within your callback which has that alert inside it or set up PubSub.
Method within callback:
remoteStorage.requestValue(stringdepuntos, function(key, value){
alert("The value for '" + key + "' is '" + value + "'");
this.val = value;
someFunction();
});
function someFunction() {
alert("outside " + remoteStorage.val);
}
but if you're doing that you may as well remove the this.val = value;
line and just do this:
remoteStorage.requestValue(stringdepuntos, function(key, value){
alert("The value for '" + key + "' is '" + value + "'");
someFunction(value);
});
function someFunction(value) {
alert("outside " + value);
}
Or simple pubsub:
<script type="text/javascript">
var pubsub = (function() {
var cache = {};
function emit() {
var e = arguments[0],
opts = Array.prototype.slice.call(arguments, 1),
len = cache[e] ? cache[e].length : false, i = 0;
for (i;i<len;i++) {
cache[e][i].apply(this, opts);
}
}
function listen(e, callback) {
cache[e] = cache[e] || [];
// push callback onto subscription
cache[e].push(callback);
}
return {
emit: emit,
listen: listen
};
}());
/*
* Copyright 2010 Nicholas C. Zakas. All rights reserved.
* BSD Licensed.
*/
function CrossDomainStorage(origin, path){
this.origin = origin;
this.path = path;
this._iframe = null;
this._iframeReady = false;
this._queue = [];
this._requests = {};
this._id = 0;
}
CrossDomainStorage.prototype = {
//restore constructor
constructor: CrossDomainStorage,
//public interface methods
init: function(){
var that = this;
if (!this._iframe){
if (window.postMessage && window.JSON && window.localStorage){
this._iframe = document.createElement("iframe");
this._iframe.style.cssText = "position:absolute;width:1px;height:1px;left:-9999px;";
document.body.appendChild(this._iframe);
if (window.addEventListener){
this._iframe.addEventListener("load", function(){ that._iframeLoaded(); }, false);
window.addEventListener("message", function(event){ that._handleMessage(event); }, false);
} else if (this._iframe.attachEvent){
this._iframe.attachEvent("onload", function(){ that._iframeLoaded(); }, false);
window.attachEvent("onmessage", function(event){ that._handleMessage(event); });
}
} else {
throw new Error("Unsupported browser.");
}
}
this._iframe.src = this.origin + this.path;
},
requestValue: function(key, callback){
var request = {
key: key,
id: ++this._id
},
data = {
request: request,
callback: callback
};
if (this._iframeReady){
this._sendRequest(data);
} else {
this._queue.push(data);
}
if (!this._iframe){
this.init();
}
},
//private methods
_sendRequest: function(data){
this._requests[data.request.id] = data;
this._iframe.contentWindow.postMessage(JSON.stringify(data.request), this.origin);
},
_iframeLoaded: function(){
this._iframeReady = true;
if (this._queue.length){
for (var i=0, len=this._queue.length; i < len; i++){
this._sendRequest(this._queue[i]);
}
this._queue = [];
}
},
_handleMessage: function(event){
if (event.origin == this.origin){
var data = JSON.parse(event.data);
this._requests[data.id].callback.call(this, data.key, data.value);
pubsub.emit('/crossdomainstorage/finish', data.key, data.value);
delete this._requests[data.id];
}
}
};
var remoteStorage = new CrossDomainStorage("http://somewhere.com", "/server.html");
remoteStorage.requestValue(stringdepuntos, function(key, value){
alert("The value for '" + key + "' is '" + value + "'");
});
pubsub.listen('/crossdomainstorage/finish', function(key, value) {
alert("outside " + value);
});
</script>