Permalink
Browse files

added date view, refactored subview markup generation

  • Loading branch information...
1 parent 5c9649a commit 106c3216d09cbf788cd62f41798b63047dc60eb6 Chris Nelson committed May 30, 2012
@@ -1,11 +1,12 @@
.modal-header
- %button{class: "close"} x
+ %a{class: "close", href: "#people/list"} x
%h3 New Person
%form.person-form
.modal-body
%fieldset
= @textField(field: "first_name", label: "First Name")
= @textField(field: "last_name", label: "Last Name")
+ = @dateField(field: "birth_date", label: "Birth Date", format: "yyyy-mm-dd")
= @selectField(field: "occupation_id", label: "Occupation", collection: @occupations)
.modal-footer
%input{type: "submit", value: "Save", class: "btn btn-primary"}
@@ -36,4 +36,4 @@ describe "EditPersonView", ->
it "should reenable the button", ->
expect(@editPersonView.$("input[type='submit']").attr("disabled")).toBeFalsy()
it "should display the error message", ->
- expect(@editPersonView.$("div[data-field=first_name]")).toHaveText /cannot be Bob/
+ expect(@editPersonView.$el).toHaveText /cannot be Bob/
@@ -1,6 +1,7 @@
#= require hamlcoffee
#= require inflection
#= require jquery.serializeObject
+#= require bootstrap-datepicker
#= require_self
#= require_tree .
@@ -1,5 +1,11 @@
class Backtastic.View extends Backbone.View
+
+ attributes: ->
+ "data-view-id": @cid
+ toHtml: ->
+ @$el.clone().wrap("<p>").parent().html()
+
render: ->
@trigger("beforeRender")
@$el.html @template(@)
@@ -0,0 +1,12 @@
+#= require ./form_field_view
+class Backtastic.Views.DateFieldView extends Backtastic.Views.FormFieldView
+
+ template: JST["templates/text_field_template"]
+
+ constructor: (options) ->
+ super
+ @format = options.format
+
+ render: ->
+ super
+ @$("input").datepicker(format: @format)
@@ -8,7 +8,7 @@ class Backtastic.Views.FormFieldView extends Backtastic.View
@parentView.on "rendered", => @afterParentRender()
afterParentRender: ->
- @setElement(@parentView.$("[data-field=#{@field}]"))
+ @setElement(@parentView.$("[data-view-id=#{@cid}]"))
@render()
render: ->
@@ -13,30 +13,26 @@ class Backtastic.Views.FormView extends Backtastic.View
errors = errors.errors if errors.errors #rails does it this way
for field, errorMessages of errors
@fieldViews[field]?.displayErrors(errorMessages)
-
- textField: (options) ->
- @fieldViews[options.field] = new Backtastic.Views.TextFieldView
+
+ fieldView: (fieldViewClass, options) ->
+ fieldView = new fieldViewClass _.extend options,
parentView: @
- field: options.field
- label: options.label
model: @model
- "<div data-field='#{options.field}'></div>"
+ @fieldViews[options.field] = fieldView
+ fieldView.toHtml()
+
+ dateField: (options) ->
+ @fieldView(Backtastic.Views.DateFieldView, options)
+
+ textField: (options) ->
+ @fieldView(Backtastic.Views.TextFieldView, options)
selectField: (options) ->
- @fieldViews[options.field] = new Backtastic.Views.SelectFieldView
- parentView: @
- field: options.field
- label: options.label
- model: @model
- collection: options.collection
- "<div data-field='#{options.field}'></div>"
-
+ @fieldView(Backtastic.Views.SelectFieldView, options)
+
save: (event)->
@$("input[type='submit']").attr("disabled", "disabled")
@clearErrors()
event.preventDefault()
@model.on "error", (model, response) => @displayErrors(response)
@model.save @$("form").serializeObject()
-
- afterSave: ->
- @$el.modal("hide")
@@ -1,7 +1,7 @@
class Backtastic.Views.SelectFieldView extends Backtastic.Views.FormFieldView
template: JST["templates/select_field_template"]
-
+
render: ->
super
@$("select").val @model.get(@field)
Oops, something went wrong.

0 comments on commit 106c321

Please sign in to comment.