In my code this.options returns object with .all object nested. Yet when I try to access it with this.options['all'] or this.options.all I get undefined.
console.log(this.options):
Object
all: Object
cfg_autoresize: "true"
cfg_autosave: "false"
cfg_monthly_target: "monthly_target"
cfg_statistics: "statistics"
cfg_ticker: "ticker"
cfg_yearly_target: "yearly_target"
__proto__: Object
Could someone help me figure this out, never had this issue before. Thanks
Edit:
content_script, simplified version
var Dashboard = Backbone.View.extend({
el: $('body'),
options: {},
get_localStorage: function() {
var _this = this;
function handle_response(response) {_this.options.all = response.data;}
chrome.extension.sendRequest({method: "allLocalStorage"}, handle_response);
},
initialize: function() {
this.get_localStorage();
console.log(this.options); // above object
console.log(this.options.all); //undefined
}
});
background.html, taken from Google-chrome-extension docs and modified
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if(request.method == "allLocalStorage") {
var options = {};
for (var i=0, l=localStorage.length; i<l; i++){
var key = localStorage.key(i);
var value = localStorage[key];
options[key] = value;
}
sendResponse({data: options});
} else {
sendResponse({});
}
});
Ideally I would like to have this.options have all options inside of it without .all but it would not work for me.
Can you show us part of the js code (declaration and object access) that seems incorrect?
EDIT:
Ah i see… at the time you call get_localStorage(); the object might not being initialized due to the fact that the handle_response function will yet not get triggered before the sendRequest finishes.
you probably want to try something like the following: