Skip to content

Commit

Permalink
Validate presence of name and category using backbone.js
Browse files Browse the repository at this point in the history
  • Loading branch information
David Francisco committed May 18, 2011
1 parent 753cf49 commit 1194763
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
7 changes: 7 additions & 0 deletions public/javascripts/backbone/models/item.js
Expand Up @@ -7,6 +7,13 @@ var Item = Backbone.Model.extend({
// If it's not new, then it should POST to /documents/id for an UPDATE action
return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
},
validate: function(attrs) {
// Check if name and category were set
if (attrs.name === "")
return "Name is empty";
if (attrs.category === "")
return "Category is empty";
},
sync: hybridSync
// localStorage: new Store("pendingItems")
});
2 changes: 1 addition & 1 deletion public/javascripts/backbone/views/edit.js
Expand Up @@ -19,7 +19,7 @@ App.Views.Edit = Backbone.View.extend({
Backbone.history.saveLocation('items/' + model.id);
},
error: function() {
new App.Views.Error();
new App.Views.Error({ message: 'Error saving this item. Did you filled all attributes?' });
}
});

Expand Down
5 changes: 3 additions & 2 deletions public/javascripts/backbone/views/notice.js
Expand Up @@ -22,12 +22,13 @@ App.Views.Notice = Backbone.View.extend({
view.remove();
});
});

return this;
}
});

App.Views.Error = App.Views.Notice.extend({
className: "error",
defaultMessage: 'Uh oh! Something went wrong. Please try again.'
defaultMessage: function(error) {
return error ? error : 'Uh oh! Something went wrong. Please try again.';
}
});

0 comments on commit 1194763

Please sign in to comment.