Last active
December 23, 2015 04:39
-
-
Save werelax/6581308 to your computer and use it in GitHub Desktop.
Backbone+R base classes
This file contains hidden or 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
| /* Base classes */ | |
| var BaseView = R.extend(Backbone.View, { | |
| init: function() { | |
| return Backbone.View.apply(this, arguments) | |
| }, | |
| render: function() { | |
| var model = (this.model || this.collection), | |
| data = model.toJSON(), | |
| html = this.template(data) | |
| this.$el.html(html) | |
| return this | |
| } | |
| }) | |
| var BaseModel = R.extend(Backbone.Model, { | |
| init: function() { | |
| return Backbone.Model.apply(this, arguments) | |
| } | |
| }) | |
| var BaseCollection = R.extend(Backbone.Collection, { | |
| init: function() { | |
| return Backbone.Collection.apply(this, arguments) | |
| } | |
| }) | |
| var SmartRouter = R.extend(Backbone.Router, { | |
| init: function(options) { | |
| options || (options = {}) | |
| this._activePage = null | |
| this.container = $(options.container || "body") | |
| return Backbone.Router.apply(this, arguments) | |
| }, | |
| changePage: function(newPage) { | |
| var oldActive = this._activePage | |
| this._activePage = newPage.render() | |
| this.container.html(this._activePage.el) | |
| this._activePage.didShow && this._activePage.didShow() | |
| if (oldActive) { | |
| oldActive.undelegateEvents() | |
| oldActive.didHide && oldActive.didHide() | |
| } | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment