Skip to content

Instantly share code, notes, and snippets.

@werelax
Last active December 23, 2015 04:39
Show Gist options
  • Select an option

  • Save werelax/6581308 to your computer and use it in GitHub Desktop.

Select an option

Save werelax/6581308 to your computer and use it in GitHub Desktop.
Backbone+R base classes
/* 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