Created
November 16, 2011 15:56
-
-
Save searls/1370452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.tooltip = $.fn.tooltip || -> | |
window.App = {} | |
App.addValidationSupportTo = (obj) -> | |
if obj instanceof Backbone.Model | |
support = | |
isValid: -> | |
result = _(@validations).all (validation,attr) => | |
validation.test.call(@,@get(attr)) | |
if result then true else false | |
validationMessages: -> | |
_(@validations).inject(((messages, validation, attr) => | |
unless validation.test.call(@,@get(attr)) | |
msg = validation.message | |
messages[attr] = if _(msg).isFunction() then msg.call(@,@get(attr)) else msg | |
messages | |
), {}) | |
else if obj instanceof Backbone.View | |
support = | |
renderValidations:-> | |
@$(':input').removeClass('invalid').removeAttr('title') | |
_(@model.validationMessages()).each (message,name) => | |
@$(":input[name=\"#{name}\"]"). | |
addClass('invalid'). | |
attr('title', message).tooltip | |
track: true, | |
delay: 200, | |
opacity: 1, | |
fixPNG: true, | |
top: 10, | |
left: 0 | |
_(support).functions (f) -> _(f).bind(obj) | |
_(obj).extend(support) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "App.addValidationSupportTo", -> | |
class PizzaView extends Backbone.View | |
initialize: -> | |
App.addValidationSupportTo(this) | |
class Pizza extends Backbone.Model | |
MAX_TOPPINGS: 5 | |
initialize: -> | |
App.addValidationSupportTo(this) | |
validations: | |
cheese: | |
test: (cheese) -> _(['american','mozarella','cheddar']).contains(cheese) | |
message: "Cheese must be american, mozarella, or cheddar" | |
toppings: | |
test: (toppings) -> toppings.length <= @MAX_TOPPINGS | |
message: (toppings) -> "Pizzas can at most have #{@MAX_TOPPINGS} toppings, but you have #{toppings.length}" | |
describe "extending backbone views", -> | |
Given -> | |
@subject = new PizzaView | |
model: new Pizza | |
cheese: 'swiss' | |
toppings: [] | |
describe "#renderValidations", -> | |
Given -> spyOn($.fn, "tooltip") | |
context "an invalid text field", -> | |
Given -> @$input = $(@subject.el).inject(el: 'input', attrs:{name: 'cheese'}) | |
When -> @subject.renderValidations() | |
Then -> expect(@$input).toHaveClass('invalid') | |
Then -> expect(@$input).toHaveAttr('title',"Cheese must be american, mozarella, or cheddar") | |
Then -> expect($.fn.tooltip).toHaveBeenCalledWith | |
track: true, | |
delay: 200, | |
opacity: 1, | |
fixPNG: true, | |
top: 10, | |
left: 0 | |
context "clearing previously invalid fields", -> | |
Given -> @subject.model.set cheese: 'cheddar' | |
When -> @subject.renderValidations() | |
Then -> expect(@$input).not.toHaveClass('invalid') | |
Then -> expect(@$input).not.toHaveAttr('title') | |
describe "extending backbone models", -> | |
Given -> @subject = new Pizza | |
describe "#isValid", -> | |
context "when valid", -> | |
Given -> @subject.set cheese: 'american', toppings: ['bacon'] | |
When -> @result = @subject.isValid() | |
Then -> @result == true | |
context "when invalid", -> | |
Given -> @subject.set cheese: 'gouda', toppings: ['bacon'] | |
When -> @result = @subject.isValid() | |
Then -> @result == false | |
describe "#validationMessages", -> | |
context "when valid", -> | |
Given -> @subject.set cheese: 'cheddar', toppings: [] | |
When -> @result = @subject.validationMessages() | |
Then -> expect(@result).toEqual({}) | |
context "when cheese is invalid", -> | |
Given -> @subject.set toppings: [] | |
When -> @result = @subject.validationMessages() | |
Then -> expect(@result).toEqual | |
cheese: "Cheese must be american, mozarella, or cheddar" | |
context "when toppings are invalid", -> | |
Given -> @subject.set cheese: 'cheddar', toppings: [0..5] | |
When -> @result = @subject.validationMessages() | |
Then -> expect(@result).toEqual | |
toppings: "Pizzas can at most have 5 toppings, but you have 6" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment