Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page change listeners #3

Merged
merged 2 commits into from May 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.html
Expand Up @@ -29,6 +29,15 @@
]};

jbackbone.init(config);

/** Track all page changes change **/
jbackbone.addPageChangeListener(function(oldPage,newPage){
console.log('Page changed from '+oldPage+' to '+newPage);
});
/** Track specific page change **/
jbackbone.addPageChangeListener(function(oldPage,newPage){
console.log('Special page is here!');
},'sample-page2');

}, false);

Expand Down
33 changes: 32 additions & 1 deletion jbackbone-mobile.js
Expand Up @@ -11,6 +11,7 @@ function JBackbone(){
this.lastPage = null;
this.currentPage = null;
this.history = [];
this.pageChangeListeners = { $all: [] };

this.x = 0;
this.width = 0;
Expand Down Expand Up @@ -38,6 +39,7 @@ JBackbone.prototype.init = function(config){
this.x = 0;
this.width = window.innerWidth;
this.history = [];
this.pageChangeListeners = { $all: [] };
this.box = document.getElementById(this.config.BOX_ID); //the container for pages
this.box.style.left = 0;

Expand Down Expand Up @@ -76,7 +78,9 @@ JBackbone.prototype.goToPage = function(nextPage, config){
if(config.addToHistory) this.history.push(this.currentPage);
if(config.resetHistory) this.history = [];

var oldPage = this.currentPage;
this.currentPage = nextPage;
this.notifyPageChange(oldPage, this.currentPage);
}

JBackbone.prototype.goBack = function(){
Expand All @@ -85,7 +89,9 @@ JBackbone.prototype.goBack = function(){
var previousPage = this.history.pop();
console.log("goBack: "+previousPage);
this.swapPage(previousPage, this.ANIM_SLIDE_RIGHT);
this.currentPage = previousPage;
var oldPage = this.currentPage;
this.currentPage = previousPage;
this.notifyPageChange(oldPage, this.currentPage);
}

JBackbone.prototype.hidePage = function(obj){
Expand Down Expand Up @@ -190,6 +196,31 @@ JBackbone.prototype.toggleMenu = function(menuPage, config){
else this.showMenu(menuPage, config);
}

JBackbone.prototype.addPageChangeListener = function(func, page){
if(typeof func != 'function') return;
if(typeof page != 'string') page = '$all';
if(!this.pageChangeListeners[page]) this.pageChangeListeners[page] = [];
this.pageChangeListeners[page].push(func);
}

JBackbone.prototype.notifyPageChange = function(oldPage, newPage){
if(typeof newPage != 'string') return;
//notify specific page
var specificListeners = this.pageChangeListeners[newPage];
if(typeof specificListeners == 'object' && Array.isArray(specificListeners)){
for(var i=0; i<specificListeners.length; ++i){
specificListeners[i](oldPage,newPage);
}
}
//notify $all
var allListeners = this.pageChangeListeners['$all'];
if(typeof allListeners == 'object' && Array.isArray(allListeners)){
for(var i=0; i<allListeners.length; ++i){
allListeners[i](oldPage,newPage);
}
}
}

JBackbone.prototype.addPage = function(id, url) {
var req = false;
// For Safari, Firefox, and other non-MS browsers
Expand Down