Last active
December 11, 2015 10:48
-
-
Save CSilivestru/4588831 to your computer and use it in GitHub Desktop.
Integrating Junior into your mobile web app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jr.Navigator = { | |
viewIdentifier: "", //I give an initial value of the view we're on a blank value. Just don't want it to start off being null | |
backButtonFlag: true, | |
... | |
... | |
navigate: function(url, opts) { | |
viewIdentifier = url; //My view identifier is the matched route used in Junior | |
this.history.push(opts); | |
this.backButtonFlag = false; | |
return Backbone.history.navigate(url, opts); | |
}, | |
... | |
... | |
renderView: function(mainEl, view) { | |
var animation, newEl; | |
animation = this.history.length > 0 ? this.history[this.history.length -1].animation : null; | |
if (this.backButtonFlag === true) { | |
Backbone.Events.trigger("back"); | |
try { | |
//We trigger the generic event name + it's identifier. Now we can hook into a per-view event! | |
//Simple but effective | |
Backbone.Events.trigger("back-" + Jr.viewIdentifier, view); | |
Jr.viewIdentifier = view.viewIdentifier; | |
} catch (e) { | |
Backbone.Events.trigger("back-" + this.viewIdentifier, view); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//In order to get rid of zombie Junior views, I extended Jr to include an AppView variable. | |
//Instead of call view.render(), we'll call AppView.showView(view, this) from your router | |
Jr.AppView = { | |
currentView: null, | |
previousView: null, | |
showView: function(view, router) { | |
if (this.currentView) | |
this.previousView = this.currentView; | |
this.currentView = view; | |
router.renderView(this.currentView); | |
if (this.previousView){ | |
var that = this; | |
setTimeout(function() { | |
that.previousView.close(); | |
}, 400); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment