Advertisement
Guest User

layout coffeescript

a guest
Mar 12th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. class Layout extends Backbone.Model
  4.  
  5.     content: "Empty"
  6.    
  7.     defaults:
  8.         active: true
  9.  
  10.     initialize: ->
  11.            
  12.     toggle: ->
  13.         @set 'active', !@get('active')
  14.  
  15.     collapse: ->
  16.         @destroy()
  17.         @view.remove()
  18.         layoutList.last().set('active',true)
  19.  
  20. class LayoutList extends Backbone.Collection
  21.  
  22.     model: Layout
  23.  
  24.     activeLayout: ->
  25.         _.filter @, (layout) -> layout.get('active') is true
  26.  
  27.     disableAll: ->
  28.         _.each @.toArray(), (layout) -> layout.set('active',false)
  29.  
  30. class LayoutView extends Backbone.View
  31.  
  32.     tagName: "div"
  33.  
  34.     template: _.template  $("#LayoutTemplate").html()
  35.  
  36.     events:
  37.         "click .expand": "expand"
  38.         "click .collapse": "collapse"
  39.  
  40.     expand: =>
  41.         newLayout = new Layout()
  42.         layoutList.add newLayout
  43.         layoutList.disableAll()
  44.         layoutList.last().set('active',true)
  45.        
  46.     collapse: ->
  47.         @model.collapse()
  48.  
  49.     initialize: ->
  50.         @model.bind 'change', @render
  51.         @model.view = @
  52.  
  53.     render: =>
  54.         templateHtml = @template
  55.             layout: @model
  56.             layoutCount: layoutList.length
  57.  
  58.         @.$el.html templateHtml
  59.         @
  60.  
  61.     toggleActive: ->
  62.         @model.toggle()
  63.  
  64.     remove: ->
  65.         $(@el).remove()
  66.  
  67. class App extends Backbone.View
  68.  
  69.     el: "#app"
  70.  
  71.     initialize : ->
  72.         layoutList.bind 'add', @addOne
  73.         layoutList.bind 'reset', @addAll
  74.         layoutList.bind 'all', @render
  75.  
  76.     addOne: (layout) =>
  77.         view = new LayoutView
  78.             model: layout
  79.             list: layoutList
  80.         @.$el.append view.render().el
  81.  
  82.     addAll: ->
  83.         layoutList.each @addOne
  84.  
  85.     render: =>
  86.         @
  87.  
  88.  
  89. layoutList = new LayoutList()
  90. app = new App
  91.  
  92. initLayout = new Layout()
  93.  
  94. layoutList.add initLayout
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement