I’m lost again.
I’m using the jquery widget factory. I declare a global object that keeps track of visited pages. I set this in one function and need to access it from another function, which I can’t get to work.
Can someone tell me, what I’m doing wrong here:
(function($,window){
$.widget("mobile.multiview",$.mobile.widget, {
vars: {
hist:{},
// there is 4 panel elements, which contain several pages each
$panel:$("div:jqmData(role='panel')")
},
_create: function() {
// access vars
var self = this;
// run through all panels, retrieve each panel ID
$panels.each(function(index) {
var id = $(this).jqmData('id');
// sorry, this was pasted to quickly
// for each panel I'm setting an object
// and adding the ID of the first page (data-show="first") to the object
self.vars.hist[id] = ['#'+$(this).find(':jqmData(role="page")').attr('id')];
console.log("entry made");
//console.log("entry: "+self.vars.hist[id][0]);
});
},
crumble: function(event, data, page) {
var self = $( '#'+page.attr('id') ),
$crumPanel = $( '#'+page.attr('id') ).closest('div:jqmData(role="panel")'),
$backup = self.vars.hist[$crumPanel.jqmData('id')][self.vars.hist[$crumPanel.jqmData('id')].length-1];
// this stays undefined...
console.log( $backup );
I hoped by declaring hist on a global level it would be possible to access the stored information, similar to other variables I create within the vars section. But it isn’t… I guess only the empty object is stored globally.
So how can I access the “local” data, filled into the object inside _create?
Thanks for help!
It was a timing issue. I set crumble to fire on
pagecreate, which fired earlier than _create. So I could not access the object elements from crumble as they were not set.By binding to
pageinitinside _create (this supercedespagecreateevent, I can call the hist object from other functions as well.I put it in the global vars to basically do just that. Define it on a widget-wide basis and make it accessible from all widget functions.