Skip to content

Instantly share code, notes, and snippets.

$ ->
App.albums = new App.Collections.Albums
App.albumsListView = new App.Views.AlbumsListView(collection: App.albums)
App.albums.fetch
success: ->
App.albumsListView.setPlaceholder()
# app/assets/javascripts/views/albums_list_view.js.coffee
App.Views.AlbumsListView = Backbone.View.extend
el: '#featured'
initialize: ->
@listenTo @collection, 'sync', @render
@on 'handlePlaceholder', @setPlaceholder, @
render: ->
for model in @collection.featured()
itemView = new App.Views.AlbumItemView(model: model, listView: this)
# app/assets/javascripts/collections/albums.js.coffee
App.Collections.Albums = Backbone.Collection.extend
model: App.Models.Album
url: ->
userId = $('#featured').data('user-id')
"/users/#{userId}/albums"
featured: ->
@where(featured: true)
App.Views.AlbumItemView = Backbone.View.extend
tagName: 'li'
template: JST['templates/album_template']
initialize: (opts) ->
@listView = opts.listView
render: ->
@$el.html @template @model.attributes
this
events:
'click .unfeature': 'unfeature'
App.AlbumItemView = Backbone.View.extend
tagName: 'li'
template: JST['templates/album_template']
//= require_tree ./templates
//= require_tree ../templates
%a{ href: "users/#{user_id}/albums/#{id}" }
= title
%button.unfeature Unfeature ×
//= require_self
//= require_tree ../templates
//= require_tree ./models
//= require_tree ./collections
//= require_tree ./views
//= require_tree ./routers
//= require_tree .
//= require app
//
window.App = {
setPlaceholder: ->
if @collection.featured().length is 0
$('.featured-albums').append('<p>No featured albums</p>')
else
$('.featured-albums').find('p').remove()