Skip to content

Instantly share code, notes, and snippets.

@Crisfole
Last active December 12, 2015 06:39
Show Gist options
  • Save Crisfole/4731104 to your computer and use it in GitHub Desktop.
Save Crisfole/4731104 to your computer and use it in GitHub Desktop.
My Backbone Base View (adds a few extra handy methods to Backbone.View) Notes: 1. I always use parentheses 2. I always include `return` except: * When returning `this` at the end of the method * When the method is a single line (in which case I like to put it in-line with the method name) 3. I tend to overuse fat-arrows. I'm totally aware of thi…
class CP.Views.BaseView extends Backbone.View
# Methods for showing and hiding the view
show: () =>
@$el.show()
@
hide: () =>
@$el.hide()
@
toggle: (show) =>
@$el.toggle.apply(@$el, arguments)
@
# Be sure to call `super()` in all subclasses' `unload` method
unload: () =>
@off()
@undelegateEvents()
@
# Methods for testing visibility and presence in Dom
# Thanks to SLaks (http://stackoverflow.com/a/3086084/456188) for the fastest way to do this.
inDom: () => $.contains(document.documentElement, @el)
isVisible: () => @$el.is(":visible")
onScreen: () => @inDom() and @isVisible()
@Crisfole
Copy link
Author

Note that I recently switched to overriding the 'remove' method rather than using unload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment