Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Backbone code that's rendering one of the models 4 extra times.
# config file
window.App =
Models: {}
Collections: {}
Routers: {}
Views: {}
init: ->
@service_providers = new App.Collections.ServiceProvidersCollection()
success: ->
App.serviceProviderRouter = new App.Routers.ServiceProvidersRouter()
Backbone.history.start(pushState: true)
window.document.addEventListener 'click', (e) ->
e = e or window.event
target = or e.srcElement
if target.nodeName.toLowerCase() is 'a'
uri = target.getAttribute('href')
App.serviceProviderRouter.navigate(uri.substr(1), true)
window.addEventListener 'popstate', (e) ->
App.serviceProviderRouter.navigate(location.pathname.substr(1), true)
$(document).ready ->
# router
class App.Routers.ServiceProvidersRouter extends Backbone.Router
"service_providers/:name_url" : "show"
"service_providers" : "index"
"service_providers/" : "index"
initialize: ->
@service_providers = App.service_providers
index: ->
@view = new App.Views.ServiceProviders.IndexView(service_providers: @service_providers)
show: (name_url) ->
@model = @service_providers.where(name_url: name_url)[0]
@view = new App.Views.ServiceProviders.ShowView(model: @model)
# index view
App.Views.ServiceProviders ||= {}
class App.Views.ServiceProviders.IndexView extends Backbone.View
template: JST["backbone/templates/service_providers/index"]
initialize: ->
addAll: () =>
addOne: (serviceProvider) =>
view = new App.Views.ServiceProviders.ServiceProviderView({model : serviceProvider})
render: =>
$(@el).html(@template(service_providers: @service_providers ))
return this
# service_provider.jst.ejs
<div class='provider'>
<a href='/service_providers/<%= name_url %>'><%= name %></a>
Industries: <%= industry_list %>
<div class='.cf'></div>
# index.jst.ejs
# just has this on opening div
# I was left with 2 closing divs if I closed this
<div class='providers'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.