Permalink
Browse files

Updated the model option to accept a functional builder.

if 'model' option is a function, #fetch_model invokes it to
get a model per form submission. This allows to create new model
instances for new forms.
  • Loading branch information...
1 parent 3f3f95c commit 13dea7f74000919c41d8c342232bdd42dfaa8917 @ratnikov ratnikov committed Feb 27, 2009
Showing with 34 additions and 9 deletions.
  1. +19 −8 src/form.js
  2. +15 −1 test/form_test.js
View
@@ -3,6 +3,7 @@ AjaxResource.Form = function(form, options) {
var self = this;
this._form = jQuery(form);
+ this._error_panel = new AjaxResource.Errors(form);
if (typeof options === "undefined") {
options = {};
@@ -32,8 +33,16 @@ AjaxResource.Form.prototype.submit_button = function() {
return this.form().find(":submit");
};
-AjaxResource.Form.prototype.model = function() {
- return this._model;
+AjaxResource.Form.prototype.error_panel = function(){
+ return this._error_panel;
+}
+
+AjaxResource.Form.prototype.fetch_model = function() {
+ if (jQuery.isFunction(this._model)) {
+ return this._model();
+ } else {
+ return this._model;
+ }
};
AjaxResource.Form.prototype.semaphore = function() {
@@ -74,18 +83,20 @@ AjaxResource.Form.prototype.parse_fields = function() {
AjaxResource.Form.prototype.submit = function() {
var self = this;
if (this.semaphore().available()) {
+ var model = this.fetch_model();
+
// update the model from the attributes within the form
- this.model().parse_json(this.parse_fields());
+ model.parse_json(this.parse_fields());
- this.model().save(function(saved_model) {
+ model.save(function(saved_model) {
if (saved_model.valid()) {
- // if the model is valid, clear errors and execute the on_saved callback
+ // if the model is valid, clear error_panel and execute the on_saved callback
- self.errors().clear();
+ self.error_panel().clear();
self.on_save(saved_model);
} else {
- // if returned model contained errors, report them
- self.errors().set(saved_model.errors());
+ // if returned model contained error_panel, report them
+ self.error_panel().set(saved_model.errors());
}
// the request was handled, decrease the semaphore
View
@@ -24,6 +24,20 @@ jQuery(document).ready(function() {
matches_only(form.submit_button(), '#submit-button', "Should match the correct button");
});
+ test("#fetch_model should return model object if 'model' option is not a function", function() {
+ var model = {};
+ equals((new AjaxResource.Form("foo", { model : model })).fetch_model(), model, "Should return the specified object itself");
+ });
+
+ test ("#fetch_model should invoke model function and return the returns if model options is a function", function() {
+ var model_builder = function() { return "foo"; };
+ equals((new AjaxResource.Form("foo", { model : model_builder })).fetch_model(), "foo", "Should return the builder's return");
+ });
+
+ test("#error_panel should return correctly specified error object", function() {
+ matches_only(form.error_panel().error_div(), "#form-error", "Should only locate error div within the form");
+ });
+
test("Clicking submit button should attempt to submit the form", function() {
var submitted = false;
form.submit = function() { submitted = true; };
@@ -111,7 +125,7 @@ jQuery(document).ready(function() {
errors.cleared = true;
}
};
- form.errors = function() { return errors; };
+ form.error_panel = function() { return errors; };
// submit the form
form.submit();

0 comments on commit 13dea7f

Please sign in to comment.