-
-
Save ericlbarnes/80f6ef0099fbe96025dc to your computer and use it in GitHub Desktop.
backbone validation
This file contains 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
App.Models.Test = Backbone.Model.extend( | |
urlRoot: '/api/test' | |
validate: (attrs) -> | |
errors = [] | |
if attrs.to is '' | |
errors.push | |
name: "to" | |
field: "js-to" | |
message: "You must enter a to address" | |
if attrs.subject is '' | |
errors.push | |
name: "subject" | |
field: "js-subject" | |
message: "You must enter a subject" | |
# Return our errors array if it isn't empty | |
errors if errors.length > 0 | |
) | |
App.Views.Details = Backbone.View.extend( | |
initialize: -> | |
@model.bind "error", @error, this | |
events: | |
"click #js-save": "saveItem" | |
saveItem: (e) -> | |
e.preventDefault() | |
# Set the model then save it. | |
@model.set | |
subject: $("#js-subject").val() | |
message: $("#js-message").val() | |
mailbox_id: $("#js-from").val() | |
to: $("#js-to").val() | |
cc: $("#js-cc").val() | |
bcc: $("#js-bcc").val() | |
tags: App.Helpers.tagsToObject $('#js-tags').val() | |
scope: $('#js-scope').val() | |
attachments: attachments | |
@model.save null, | |
success: (model, response) => | |
App.Helpers.showAlert "Success!", "Saved Successfully", "alert-success" | |
@next() | |
error: (model, response) -> | |
App.Helpers.showAlert "Error", "An error occurred while trying to save this item", "alert-error" | |
# Show the errors based on validation failure. | |
error: (model, error) -> | |
App.Helpers.displayValidationErrors error | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment