summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/views/my/cakes/show_view.js.coffee
blob: e1c1183df313a63aab54c2e8a055288b7e41a6a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#= require views/my/cakes/thumbnail_view

class csx.Views.My.Cakes.ShowView extends Marionette.CompositeView
  template: JST["templates/my/cakes/show"]
  childView: csx.Views.My.Cakes.ThumbnailView
  childViewContainer: '.card-columns'

  events:
    "click .add-photo": "launchAddPhoto"
    "click #remove-cake-button": "removeCake"

  templateHelpers:
    hasImage: ->
      typeof(@photos) != 'undefined' && _.any(@photos)

    randomPhoto: ->
      if @primary_photo_id
        _.find @photos, (photo) =>
          photo.id.toString() == @primary_photo_id
      else
        @photos[Math.floor(Math.random()*@photos.length)]

  constructor: (options) ->
    super(options)
    @collection = @model.photos()
    @model.set('primary_photo_id', options.photo_id) if options.photo_id

  launchAddPhoto: ->
    @displayModal(new csx.Views.Photos.NewModalView(cake: @model))

  removeCake: ->
    @displayModal(new csx.Views.Cakes.DeleteCakeModalView(model: @model))

  displayModal: (view) ->
    $("#modal").html(view.render().el)
    $("#modal").modal()