Permalink
Browse files

refactoring to have field views responsible for updating the model on…

… blur
  • Loading branch information...
1 parent 9fe9c25 commit 185be06fb7d95a0ad75fcc6550fd5501fc694750 Chris Nelson committed Sep 8, 2012
@@ -24,4 +24,21 @@ describe "check box view", ->
@checkBoxFieldView.render()
it "render an unchecked check box if the value is false", ->
expect(@checkBoxFieldView.$("input[type=checkbox]").attr("checked")).toBeFalsy()
+
+ describe "updating model", ->
+ describe "when checking", ->
+ beforeEach ->
+ @checkBoxFieldView.render()
+ @checkBoxFieldView.$("input").attr("checked", true)
+ @checkBoxFieldView.updateModel()
+ it "should update the model", ->
+ expect(@model.get("awesome")).toBeTruthy()
+ describe "when unchecking", ->
+ beforeEach ->
+ @model.set awesome: true
+ @checkBoxFieldView.render()
+ @checkBoxFieldView.$("input").attr("checked", false)
+ @checkBoxFieldView.updateModel()
+ it "should update the model", ->
+ expect(@model.get("awesome")).toBeFalsy()
@@ -26,3 +26,9 @@ describe "select field view", ->
it "puts a label on it", ->
expect(@selectFieldView.$("label[for=occupation_id]")).toExist()
expect(@selectFieldView.$("label[for=occupation_id]")).toHaveText /Occupation/
+ describe "updating model", ->
+ beforeEach ->
+ @selectFieldView.$("select").val("1")
+ @selectFieldView.updateModel()
+ it "updates the model", ->
+ expect(@person.get("occupation_id")).toEqual "1"
@@ -16,6 +16,12 @@ describe "text field view", ->
expect(@textFieldView.$("label[for=name]")).toExist()
expect(@textFieldView.$("label[for=name]")).toHaveText /Name/
+ describe "updating model", ->
+ beforeEach ->
+ @textFieldView.$("input").val("Not bob")
+ @textFieldView.updateModel()
+ it "should update the model", ->
+ expect(@model.get("name")).toEqual "Not bob"
describe "displaying errors", ->
beforeEach ->
@textFieldView.displayErrors
@@ -5,4 +5,7 @@ class Backtastic.Views.CheckBoxView extends Backtastic.Views.FormFieldView
render: ->
super
- @$('input').prop("checked", "checked") if @model.get(@field)
+ @$('input').prop("checked", "checked") if @model.get(@field)
+
+ updateModel: ->
+ @model.set @field, @$("input").attr("checked") == "checked"
@@ -8,6 +8,12 @@ class Backtastic.Views.FormFieldView extends Backtastic.View
@parentView.on "rendered", => @afterParentRender()
@model.on "error", (model, errors) => @displayErrors(errors)
+ events:
+ "blur input": "updateModel"
+
+ updateModel: ->
+ @model.set @field, @$("input").val()
+
afterParentRender: ->
@setElement(@parentView.$("[data-view-id=#{@cid}]"))
@render()
@@ -5,4 +5,7 @@ class Backtastic.Views.SelectFieldView extends Backtastic.Views.FormFieldView
render: ->
super
@$("select").val @model.get(@field)
+
+ updateModel: ->
+ @model.set @field, @$("select").val()

0 comments on commit 185be06

Please sign in to comment.