Permalink
Browse files

Added html view functionality.

  • Loading branch information...
ratnikov committed Feb 25, 2009
1 parent a8d3afe commit 755d64b7c388c748d72d1c2c698f978e78eb2025
Showing with 48 additions and 0 deletions.
  1. +24 −0 src/base.js
  2. +1 −0 test/base_test.html
  3. +23 −0 test/base_test.js
View
@@ -26,6 +26,7 @@ AjaxResource.Base.prototype._assign_routes = function(spec) {
jQuery.extend(AjaxResource.Base.prototype, AjaxResource.AttributeMod.prototype);
jQuery.extend(AjaxResource.Base.prototype, AjaxResource.Routing.prototype);
+jQuery.extend(AjaxResource.Base.prototype, AjaxResource.View.prototype);
AjaxResource.Base.prototype.resource_name = function() {
return this._resource_name;
@@ -39,3 +40,26 @@ AjaxResource.Base.prototype.serialized_attributes = function() {
});
return serialized;
};
+
+AjaxResource.Base.prototype.parse_json = function(json) {
+ var resource_json = json[this.resource_name()];
+
+ if (typeof resource_json !== "undefined") {
+
+ if (typeof resource_json.html !== "undefined") {
+
+ // set to use custom html if specified
+ this.set_custom(resource_json.html);
+ delete resource_json.html;
+ } else {
+ }
+
+ // update the attributes with the resource_json
+ this.update_attributes(resource_json);
+
+ return true;
+ } else {
+ // if no resource json was parsed, return false
+ return false;
+ }
+};
View
@@ -7,6 +7,7 @@
<script src="../src/attribute_mod.js"></script>
<script src="../src/routing.js"></script>
+ <script src="../src/view.js"></script>
<script src="../lib/testrunner.js"></script>
<link rel="stylesheet" href="../lib/testsuite.css" />
View
@@ -39,4 +39,27 @@ jQuery(document).ready(function() {
test("Should return an property set of attributes serialized in 'resource[field] : value' format", function() {
same(model.serialized_attributes(), { 'funky_resource[foo]' : "foo", 'funky_resource[bar]' : "bar\nzen" });
});
+
+ module("#parse_json", {
+ setup : function() {
+ model = new AjaxResource.Base({resource : 'funky_resource' });
+ }
+ });
+
+ test("Should successfully parse a json with the resource name specified", function() {
+ ok(model.parse_json({ funky_resource : { foo : 'foo', id : 5, '12345' : 12345 } }), "Should manage to parse the json");
+ same(model.attributes(), { foo: 'foo', id: 5, '12345' : 12345 }, "Should have updated attributes");
+
+ ok(model.parse_json({ funky_resource : { foo : 'bar', errors : "bad foo" } }), "Should manage to parse with errors");
+ equals(model.attributes().foo, 'bar', "should have updated the specified attribute");
+ equals(model.attributes()['12345'], '12345', "Should have not changed former attributes that were not specified in json");
+ equals(model.errors(), "bad foo", "Should have set the errors");
+
+ equals(model.html(), model.default_view(), "Should use the default view to generate html");
+ });
+
+ test("Should parse html if specified", function() {
+ ok(model.parse_json({funky_resource : { foo : 'foo', html : '<p>foo</p>' } }), "Should parse json");
+ equals(model.html(), "<p>foo</p>", "Should use specified html as model's html");
+ });
});

0 comments on commit 755d64b

Please sign in to comment.