Skip to content

Instantly share code, notes, and snippets.

View pewniak747's full-sized avatar

Tomasz Pewiński pewniak747

View GitHub Profile
# backbone
class PostView extends Backbone.View
className: 'post'
initialize: ->
@model.on('change:rating', @ratingChanged)
teardown: ->
@model.off('change:rating')
# custom plugin cleanup here
@remove()
render: ->
# backbone
class PostView extends Backbone.View
className: 'post'
initialize: ->
@model.on('change:rating', @ratingChanged)
teardown: ->
@model.off('change:rating')
# custom plugin cleanup here
@remove()
render: ->
$('#region').html((new PostView(model: model)).render()) # => will render blank in marionette
$('#region').html((new PostView(model: model)).render().$el) # => OK
# backbone
class PostsView extends Backbone.View
initialize: ->
@collection.on('reset add remove', @render)
@collection.on('request', @startFetch)
teardown: ->
@collection.off('reset add remove', @render)
@collection.off('request' @startFetch)
render: ->
[email protected]()
class PostsWithPaginationView extends Marionette.CompositeView
itemView: PostView
emptyView: NoPostsView
itemViewContainer: '.items'
template: -> "<div class='items'></div><div class='pagination'>"
onRender: ->
# initialize pagination plugin
class PostDashboardView extends Marionette.Layout
template: -> "<div id='posts-region'></div><div id='new-post-region'></div>"
regions:
postsRegion: '#posts-region'
newPostRegion: '#new-post-region'
layout = new PostDashboardView
postsView = new PostsView(collection: new Posts)
newPostView = new NewPostView
class MainLayout extends Marionette.Layout
template: -> "<div id='main-region'></div>"
regions:
mainRegion: '#main-region'
# application startup
App.mainLayout = new MainLayout
$("body").html(@mainLayout.render().$el)
# somewhere in a router
Application = new Marionette.Application
# this will replace your mainLayout
Application.addRegions
mainRegion: 'body'
Application.on 'initialize:after', ->
Backbone.history.start()
# OtherView definition
Application.module 'Posts', (Posts, Application, Backbone, Marionete, $, _) ->
Posts.Router = Marionette.AppRouter.extend
appRoutes:
'post/:id': 'show'
class Posts.Controller
show: (id)->
post = new Post(id: id)
view = new Posts.Views.Post(model: post)
Application.mainRegion.show(view)
# define responses
Application.reqres.setHandler "post:new", ->
new Post
Application.reqres.setHandler "post:get", (id) ->
post = new Post(id: id)
post.fetch()
post