Browse files

Fixed to allow custom constructor for models.

  • Loading branch information...
1 parent 1700c31 commit cda0c6ce5b95af489f065eb6a130acb5e308b194 @ratnikov committed Feb 24, 2009
Showing with 33 additions and 13 deletions.
  1. +2 −1 spec/new_form.html
  2. +28 −7 spec/new_form_spec.js
  3. +3 −5 src/new_form.js
View
3 spec/new_form.html
@@ -1,6 +1,7 @@
<html>
<head>
<script src="../lib/jquery-1.3.1.js"></script>
+ <script src="../lib/jquery.metadata.min.js"></script>
<script src="../lib/jquery.json-1.3.min.js"></script>
<script src="spec_helper.js"></script>
@@ -22,7 +23,7 @@
</ul>
</div>
<div>
- <div id="parse-form">
+ <div id="parse-form" class="{ 'bar_id' : '5' }">
<div class="error" style="display: none">
<p>Errors:</p>
<ul>
View
35 spec/new_form_spec.js
@@ -7,11 +7,24 @@ var Util = function() {
};
}();
-var Foo = function() {
+var Foo = function(spec) {
+ var private = {};
this.attributes = {};
+
+ if (typeof(spec.bar_id) !== "undefined") {
+ this.attributes.bar_id = spec.bar_id;
+ } else {
+ throw("Must specify bar id");
+ }
+
+ this.bar_id = function() {
+ return this.attributes.bar_id;
+ };
+
AjaxResource.Base.extend(this, {
resource_name: 'foo',
- singular_path: '/foo'
+ singular_path: '/foo',
+ prefix: '/bars/'+this.bar_id()
});
var old_create = this.create;
@@ -20,7 +33,7 @@ var Foo = function() {
this.create = function(callback) {
var request = old_create(callback);
request.json_success();
- }
+ };
};
AjaxTools.read_json = function() {
@@ -29,21 +42,29 @@ AjaxTools.read_json = function() {
} else {
return jQuery.evalJSON(jQuery(".foo-error").attr("value"));
}
-}
+};
jQuery(document).ready(function() {
+ var form = jQuery("#parse-form");
+
+ var model_builder = function() {
+ return new Foo({
+ bar_id: form.metadata().bar_id
+ });
+ };
+
// using a global to be able to access from firebug
- new_form = new AjaxResource.NewForm("#parse-form", {
+ new_form = new AjaxResource.NewForm(form, {
on_create: function(model) {
alert("Create new model.");
console.log(model);
},
- model: Foo
+ model_builder: model_builder
});
jQuery(":button.parse-attributes").click(function() {
- var foo = new Foo();
+ var foo = model_builder();
new_form.parse(foo);
jQuery(".foo-success").attr("value", jQuery.toJSON({ 'foo' : foo.attributes }));
View
8 src/new_form.js
@@ -27,12 +27,10 @@ AjaxResource.NewForm = function(form, options) {
}
});
- if (typeof(options.model) !== "undefined") {
- private.new_model = function() {
- return new options.model();
- };
+ if (typeof(options.model_builder) !== "undefined") {
+ private.new_model = options.model_builder;
} else {
- throw("Need to specify model for the form");
+ throw("Need to specify model builder for the form");
}
if (typeof(options.on_create) !== "undefined") {

0 comments on commit cda0c6c

Please sign in to comment.