blob: efc7711042a42b69d5455372e3f69ff905790df1 (
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
class csx.Views.My.Cakes.NewView extends Marionette.ItemView
template: JST["templates/my/cakes/new"]
ui:
name: "#cake_name"
category: "#cake_category_id"
save_button: '#save-button'
modelEvents:
'invalid': 'displayError'
events:
"keyup input": "refreshStatus"
"change select": "refreshStatus"
"submit #new-cake": "save"
constructor: (options) ->
super(_.extend(options, { model: new options.collection.model() }))
save: (e) ->
e.preventDefault()
e.stopPropagation()
@disableSaveButton()
@collection.create(@model,
success: @savedSuccessfully
error: @couldNotSave
)
onRender: ->
@$("#cake_category_id").val($("#cake_category_id option:first").val())
@model.isValid()
savedSuccessfully: (cake) =>
window.location.hash = "cakes/#{cake.id}/edit"
couldNotSave: (cake, xhr) =>
@enableSaveButton()
error = new csx.Views.ErrorView
el: @$('form#new-cake'),
attributesWithErrors: $.parseJSON(xhr.responseText)
error.render()
refreshStatus: ->
@enableSaveButton()
@model.set('name', @ui.name.val())
@model.set('category_id', @ui.category.val())
@model.isValid()
displayError: (model, error) ->
@disableSaveButton()
enableSaveButton: ->
@ui.save_button.removeAttr('disabled')
disableSaveButton: ->
@ui.save_button.attr('disabled', 'disabled')
serializeData: ->
{
cake: @model.toJSON(),
categories: csx.Collection.Category.toJSON(),
}
|