Skip to content

Instantly share code, notes, and snippets.

@eastridge
Forked from crtr0/application.coffee
Created July 18, 2012 18:52
Show Gist options
  • Save eastridge/3138049 to your computer and use it in GitHub Desktop.
Save eastridge/3138049 to your computer and use it in GitHub Desktop.
Application = Thorax.Application.create()
# convenience for debugging
window.Application = Application
# main view controller
Application.ViewController.create
parent: Application
routes:
'': 'index'
'edit/:id': 'edit'
'create' : 'create'
index: ->
eventsView = EventsView.create
eventsCollection: Events.instance()
@setView eventsView
edit: (id) ->
events = Events.instance()
event = events.find (event) ->
event.get('id') is id
@setView EditView.create model: event
create: ->
@setView CreateView.create()
MyEvent = Application.model 'event'
defaults: {
name: ""
}
Events = Application.collection 'events'
model: MyEvent
url: '/events'
EventsView = Application.view 'events'
edit: (event) ->
model = $(event.target).model()
Backbone.history.navigate 'edit/' + model.get('id'), trigger: true
create: ->
model = new MyEvent()
Backbone.history.navigate 'create', trigger: true
template: """
<h1>Event List</h1>
{{#collection eventsCollection tag="ul"}}
<li>{{#button "edit" class="btn"}}Edit{{/button}} {{name}}</li>
{{/collection}}
{{#button "create" class="btn"}}New{{/button}}
"""
EditView = Application.view 'edit'
events: ->
'submit form': (event) ->
@serialize event, (attributes, release) =>
@model.save()
release()
template: """
<h1>Editing {{name}}</h1>
<form>
{{input name="name"}}
<input type="submit">
</form>
{{#link ""}}Go Back{{/link}}
"""
CreateView = Application.view 'create'
events: ->
'submit form': (event) ->
@serialize event, (attributes, release) =>
Events.instance().create attributes, success: ->
Backbone.history.navigate '', trigger: true
release()
template: """
<h1>New Event</h1>
<form>
{{input name="name"}}
<input type="submit" class="btn btn-primary">
</form>
{{#link ""}}Go Back{{/link}}
"""
# spin until data loaded
$ ->
document.body.appendChild(Application.el)
Application.setView Application.View.create
template: "Loading"
Events.instance().fetch success: ->
Application.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment