summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/models/cake.js.coffee
blob: 7712ac7bf56a851b9d689e3fa4c28a43922a1e07 (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
class csx.Models.Cake extends Backbone.Model
  paramRoot: 'cake'

  defaults:
    id: null
    name: null
    story: null
    created_at: null
    updated_at: null
    category_id: null

  validate: (attributes, options) ->
    return "Name can't be blank" unless attributes.name && attributes.name.trim()
    return "Category can't be blank" unless attributes.category_id

  photos: ->
    cake_id = @id
    new Backbone.Collection(csx.Collections.Photo.filter((photo) => photo.get("cake_id") == cake_id))

  public_url: ->
    "#{window.location.origin}/creations/#{@get('slug')}"

  category_id: ->
    @get('category_id') || @get('category').id

class csx.Collections.CakesCollection extends Backbone.Collection
  model: csx.Models.Cake
  url: '/api/v1/cakes'

csx.Models.Creation = csx.Models.Cake