Skip to content

Instantly share code, notes, and snippets.

@marcelbeumer
Last active December 16, 2015 13:49
Show Gist options
  • Save marcelbeumer/5444355 to your computer and use it in GitHub Desktop.
Save marcelbeumer/5444355 to your computer and use it in GitHub Desktop.
Backbone BaseView
define ["backbone", "underscore"], (Backbone, _) ->
# BaseView
# --------
class BaseView extends Backbone.View
# Optional delegate to implement delegation pattern:
# @delegate.getMonkeyCollection()
delegate: null
# Optional reference to single model.
model: null
# Optional reference to single colleciton
collection: null
# Optional reference to parent view
# @parent.getWhatINeed()
parent: null
# Named references to DOM elements by CSS selector. Format:
# "submitButton": "button.submit"
# "loginButton": "button.login"
# After render, the @resetSelectors will enable API:
# @sel.loginButton().// $API
selectors: {}
# Optional Serializer function that returns object for template
# rendering. Alternatively, the serialize method can be overriden
serializer: null
# Optional template function
template: null
# Initializer function that takes properties from options
initialize: (options = {}) ->
{@delegate, @model, @collection, @parent} = options
# Reset selectors. Creates @sel API with cached element
# functions.
resetSelectors: ->
@sel = {}
for name, selector of @selectors
do (name, selector) =>
@sel[name] = _.memoize(=>
@$(selector).first())
@
# Assigns view to selector or element using
# Backbone setElement, and (re)renders the
# view. Convenience function for subviews:
# @assign @footerView @sel.footer()
assign: (view, selectorOrElement) ->
element = selectorOrElement
if typeof element == "string"
element = @$(element)
else
element = $(element)
(view.setElement element).render()
# Override or set serializer function.
# Default implementation tries to
# run the serializer property, toJSON
# of @model or @collection, or returns
# empty object if all fails
serialize: ->
if @serializer?
return @serializer()
else
obj = @model or @collection
if obj then obj.toJSON() else {}
# Render template with object form serialize and
# reset selectors
render: ->
@$el.html @template(@serialize()) if @template
@resetSelectors()
@
# Set element now also sets "view" data
# property, so we can see if a DOM element
# is owned by certain view.
setElement: ->
# Clean up old data property (if ours)
if @$el and (@$el.data "view") is @
@$el.data null
# Do what we normally do
r = super
# Store this instance on the view data property
@$el.data "view", @
# Set selected
@select if @selected
# Return as super
return r
# Get view that belongs to an element. Can
# be called as static (BaseView::getViewForElement)
getViewForElement: (element) ->
element = ($ element)[0]
while element
view = ($ element).data "view"
return view if view
element = element.parentNode
# Check s element belongs to view
containsElement: (element, includeSubviews=true) ->
element = ($ element)[0]
while element
view = ($ element).data "view"
if view
if view is @
return true
else if not includeSubviews
return no
element = element.parentNode
return no
# Simple select
select: ->
@selected = true
@$el.addClass "selected"
# Simple deselect
deselect: ->
@selected = false
@$el.removeClass "selected"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment