Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed to allow custom constructor for models.

  • Loading branch information...
commit cda0c6ce5b95af489f065eb6a130acb5e308b194 1 parent 1700c31
Dmitry Ratnikov authored
3  spec/new_form.html
... ... @@ -1,6 +1,7 @@
1 1 <html>
2 2 <head>
3 3 <script src="../lib/jquery-1.3.1.js"></script>
  4 + <script src="../lib/jquery.metadata.min.js"></script>
4 5 <script src="../lib/jquery.json-1.3.min.js"></script>
5 6 <script src="spec_helper.js"></script>
6 7
@@ -22,7 +23,7 @@
22 23 </ul>
23 24 </div>
24 25 <div>
25   - <div id="parse-form">
  26 + <div id="parse-form" class="{ 'bar_id' : '5' }">
26 27 <div class="error" style="display: none">
27 28 <p>Errors:</p>
28 29 <ul>
35 spec/new_form_spec.js
@@ -7,11 +7,24 @@ var Util = function() {
7 7 };
8 8 }();
9 9
10   -var Foo = function() {
  10 +var Foo = function(spec) {
  11 + var private = {};
11 12 this.attributes = {};
  13 +
  14 + if (typeof(spec.bar_id) !== "undefined") {
  15 + this.attributes.bar_id = spec.bar_id;
  16 + } else {
  17 + throw("Must specify bar id");
  18 + }
  19 +
  20 + this.bar_id = function() {
  21 + return this.attributes.bar_id;
  22 + };
  23 +
12 24 AjaxResource.Base.extend(this, {
13 25 resource_name: 'foo',
14   - singular_path: '/foo'
  26 + singular_path: '/foo',
  27 + prefix: '/bars/'+this.bar_id()
15 28 });
16 29
17 30 var old_create = this.create;
@@ -20,7 +33,7 @@ var Foo = function() {
20 33 this.create = function(callback) {
21 34 var request = old_create(callback);
22 35 request.json_success();
23   - }
  36 + };
24 37 };
25 38
26 39 AjaxTools.read_json = function() {
@@ -29,21 +42,29 @@ AjaxTools.read_json = function() {
29 42 } else {
30 43 return jQuery.evalJSON(jQuery(".foo-error").attr("value"));
31 44 }
32   -}
  45 +};
33 46
34 47 jQuery(document).ready(function() {
35 48
  49 + var form = jQuery("#parse-form");
  50 +
  51 + var model_builder = function() {
  52 + return new Foo({
  53 + bar_id: form.metadata().bar_id
  54 + });
  55 + };
  56 +
36 57 // using a global to be able to access from firebug
37   - new_form = new AjaxResource.NewForm("#parse-form", {
  58 + new_form = new AjaxResource.NewForm(form, {
38 59 on_create: function(model) {
39 60 alert("Create new model.");
40 61 console.log(model);
41 62 },
42   - model: Foo
  63 + model_builder: model_builder
43 64 });
44 65
45 66 jQuery(":button.parse-attributes").click(function() {
46   - var foo = new Foo();
  67 + var foo = model_builder();
47 68 new_form.parse(foo);
48 69
49 70 jQuery(".foo-success").attr("value", jQuery.toJSON({ 'foo' : foo.attributes }));
8 src/new_form.js
@@ -27,12 +27,10 @@ AjaxResource.NewForm = function(form, options) {
27 27 }
28 28 });
29 29
30   - if (typeof(options.model) !== "undefined") {
31   - private.new_model = function() {
32   - return new options.model();
33   - };
  30 + if (typeof(options.model_builder) !== "undefined") {
  31 + private.new_model = options.model_builder;
34 32 } else {
35   - throw("Need to specify model for the form");
  33 + throw("Need to specify model builder for the form");
36 34 }
37 35
38 36 if (typeof(options.on_create) !== "undefined") {

0 comments on commit cda0c6c

Please sign in to comment.
Something went wrong with that request. Please try again.