Skip to content
Browse files

Add custom view/model/collection modules.

  • Loading branch information...
1 parent 4ae1d14 commit acc423cc85fa56dccb2d589b65dbaffabef6a2cf @elving committed Dec 11, 2012
Showing with 184 additions and 88 deletions.
  1. +1 −1 app/initialize.coffee
  2. +3 −0 app/lib/collection.coffee
  3. +93 −0 app/lib/model.coffee
  4. +85 −14 app/lib/view.coffee
  5. +0 −71 app/lib/view_collection.coffee
  6. +2 −2 app/views/app_view.coffee
View
2 app/initialize.coffee
@@ -8,7 +8,7 @@ Hipster.Collections ?= {}
$ ->
# Load App Helpers
- require '../lib/app_helpers'
+ require 'lib/app_helpers'
# Initialize App
Hipster.Views.AppView = new AppView = require 'views/app_view'
View
3 app/lib/collection.coffee
@@ -0,0 +1,3 @@
+module.exports = class Collection extends Backbone.Collection
+ resetSilent: (models) ->
+ @reset(models, silent: yes)
View
93 app/lib/model.coffee
@@ -0,0 +1,93 @@
+module.exports = class Model extends Backbone.Model
+ setSilent: (attributes) ->
+ @set(attributes, { silent: yes })
+
+ push: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr.push(values...)
+ obj[attribute] = attr
+
+ @set(obj)
+
+ pop: (attribute) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr.pop()
+ obj[attribute] = attr
+
+ @set(obj)
+
+ reverse: (attribute) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr.reverse()
+ obj[attribute] = attr
+
+ @set(obj)
+
+ shift: (attribute) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr.shift()
+ obj[attribute] = attr
+
+ @set(obj)
+
+ unshift: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr.unshift(values...)
+ obj[attribute] = attr
+
+ @set(obj)
+
+ splice: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr.splice(values...)
+ obj[attribute] = attr
+
+ @set(obj)
+
+ add: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr += value for value in values
+ obj[attribute] = attr
+
+ @set(obj)
+
+ subtract: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr -= value for value in values
+ obj[attribute] = attr
+
+ @set(obj)
+
+ divide: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr /= value for value in values
+ obj[attribute] = attr
+
+ @set(obj)
+
+ multiply: (attribute, values...) ->
+ obj = {}
+ attr = @get(attribute)
+
+ attr *= value for value in values
+ obj[attribute] = attr
+
+ @set(obj)
View
99 app/lib/view.coffee
@@ -1,27 +1,98 @@
+Model = require 'lib/model'
+
module.exports = class View extends Backbone.View
- tagName: 'section'
+ debug: off
+
+ startDebugging: ->
+ @on "#{@cid}:initialize", -> console.debug "Initialized #{@name}", @
+ @on "#{@cid}:render", -> console.debug "Rendered #{@name}", @
+ @on "#{@cid}:update", -> console.debug "Updated #{@name}", @
+ @on "#{@cid}:destroy", -> console.debug "Destroyed #{@name}", @
+
+ type: 'view'
+
+ name: null
+
+ autoRender: off
+
+ rendered: no
+
+ model: new Model()
+
+ template: -> ''
+
+ # jQuery Shortcuts
+ html: (dom) ->
+ @$el.html(dom)
+ @trigger "#{@cid}:#{if @rendered then 'update' else 'render'}", @
+ @$el
+
+ append: (dom) ->
+ @$el.append(dom)
+ @trigger "#{@cid}:#{if @rendered then 'update' else 'render'}", @
+ @$el
+
+ prepend: (dom) ->
+ @$el.prepend(dom)
+ @trigger "#{@cid}:#{if @rendered then 'update' else 'render'}", @
+ @$el
- template: ->
+ after: (dom) ->
+ @$el.after(dom)
+ @trigger "#{@cid}:update", @
+ @$el
+
+ before: (dom) ->
+ @$el.after(dom)
+ @trigger "#{@cid}:update", @
+ @$el
+
+ css: (css) ->
+ @$el.css(css)
+ @trigger "#{@cid}:update", @
+ @$el
+
+ find: (selector) ->
+ @$el.find(selector)
+
+ delegate: (event, selector, handler) ->
+ handler = selector if arguments.length is 2
+ handler = (handler).bind @
+
+ if arguments.length is 2
+ @$el.on event, handler
+ else
+ @$el.on event, selector, handler
+
+ # Use bootstrap method instead of initialize
+ bootstrap: ->
initialize: ->
- @render()
+ @bootstrap()
+
+ @name = @name or @constructor.name
+ @startDebugging() if @debug is on
+ @render() if @autoRender is on
+
+ @trigger "#{@cid}:initialize", @
getRenderData: ->
@model?.toJSON()
render: ->
- # console.debug "Rendering #{@constructor.name}", @
- @beforeRender()
- @$el.html @template @getRenderData()
- @afterRender()
+ @trigger "#{@cid}:render:before", @
+
+ @$el.attr('data-cid', @cid)
+ @html @template(@getRenderData())
+ @rendered = yes
+
+ @trigger "#{@cid}:render:after", @
@
- beforeRender: ->
+ destroy: (keepDOM = no) ->
+ @trigger "#{@cid}:destroy:before", @
- afterRender: ->
+ if keepDOM then @dispose() else @remove()
+ @model?.destroy()
- destroy: ->
- @undelegateEvents()
- @$el.removeData().unbind()
- @remove()
- Backbone.View::remove.call @
+ @trigger "#{@cid}:destroy:after", @
View
71 app/lib/view_collection.coffee
@@ -1,71 +0,0 @@
-View = require './view'
-
-class ViewCollection extends View
- collection: new Backbone.Collection()
-
- view: new View()
-
- views: []
-
- length: ->
- @views.length
-
- add: (views, options = {}) ->
- views = if _.isArray(views) then views.slice() else [views]
- for view in views
- unless @get view.cid
- @views.push(view)
- @trigger('add', view, @) unless options.silent
- @
-
- get: (cid) ->
- @find((view) -> view.cid is cid) or null
-
- remove: (views, options = {}) ->
- views = if _.isArray(views) then views.slice() else [views]
- for view in views
- @destroy(view)
- @trigger('remove', view, @) unless options.silent
- @
-
- destroy: (view = @, options = {}) ->
- _views = @filter(_view) -> view.cid isnt _view.cid
- @views = _views
- view.undelegateEvents()
- view.$el.removeData().unbind()
- view.remove()
- Backbone.View::remove.call view
- @trigger('remove', view, @) unless options.silent
- @
-
- reset: (views, options = {}) ->
- views = if _.isArray(views) then views.slice() else [views]
- @destroy(view, options) for view in @views
- if views.length isnt 0
- @add(view, options) for view in views
- @trigger('reset', view, @) unless options.silent
- @
-
- renderOne: (model) =>
- view = new @view model: model
- @$el.append view.render().el
- @add view
- @
-
- renderAll: ->
- @collection.each @renderOne
- @
-
-# Underscore methods that we want to implement on the Collection.
-methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find',
- 'detect', 'filter', 'select', 'reject', 'every', 'all', 'some', 'any',
- 'include', 'contains', 'invoke', 'max', 'min', 'sortBy', 'sortedIndex',
- 'toArray', 'size', 'first', 'initial', 'rest', 'last', 'without', 'indexOf',
- 'shuffle', 'lastIndexOf', 'isEmpty', 'groupBy']
-
-# Mix in each Underscore method as a proxy to `ViewCollection#views`.
-_.each methods, (method) ->
- ViewCollection::[method] = ->
- _[method].apply _, [@views].concat(_.toArray(arguments))
-
-module.exports = ViewCollection
View
4 app/views/app_view.coffee
@@ -1,5 +1,5 @@
-View = require '../lib/view'
-AppRouter = require '../routers/app_router'
+View = require 'lib/view'
+AppRouter = require 'routers/app_router'
module.exports = class AppView extends View
el: 'body.application'

0 comments on commit acc423c

Please sign in to comment.
Something went wrong with that request. Please try again.