Permalink
Browse files

Added nullobject's fix for checkNew with JSON, with test coverage.

  • Loading branch information...
1 parent e699f08 commit 0425a9cb5a26e9422891fac5890a429190183d38 Eric Mill committed May 1, 2008
Showing with 16 additions and 5 deletions.
  1. +9 −5 jester.js
  2. +7 −0 test/jester_test.html
View
@@ -75,24 +75,28 @@ Object.extend(Jester.Resource, {
eval('new_model._' + url + '_url = function(params) {return this._url_for("' + url + '", params);}');
if (options.checkNew)
- this.buildAttributes(new_model, options.asynchronous);
+ this.buildAttributes(new_model, options);
if (window)
window[model] = new_model;
return new_model;
},
- buildAttributes: function(model, async)
- {
+ buildAttributes: function(model, options) {
model = model || this;
+ var async = options.asynchronous;
+
if (async == null)
async = true;
var buildWork = bind(model, function(doc) {
- this._attributes = this._attributesFromTree(doc[this._singular_xml]);
+ if (this._format == "json")
+ this._attributes = this._attributesFromJSON(doc);
+ else
+ this._attributes = this._attributesFromTree(doc[this._singular_xml]);
});
- model.requestAndParse("xml", buildWork, model._new_url(), {asynchronous: async});
+ model.requestAndParse(options.format, buildWork, model._new_url(), {asynchronous: async});
},
loadRemoteJSON : function(url, callback, user_callback) {
@@ -92,6 +92,7 @@
'admin' : { '@type' : 'boolean', '#text' : 'false' },
'created_at' : { '@type' : 'datetime' }
}
+ newUserJSON = '{attributes: {first_name: null, admin: null, created_at: null}}'
Resource.requestAndParse = function (format, callback, url, options, user_callback, remote) {
if (remote && format == "json") {
@@ -149,6 +150,7 @@
Internet[User._show_url(1)] = ericJSON;
Internet[Post._show_url(1)] = postJSON;
Internet[User._list_url()] = usersJSON;
+ Internet[User._new_url()] = newUserJSON;
// Path prefixed models
Resource.model("Post", {prefix: "/users/:user_id"})
@@ -551,6 +553,11 @@
var eric = User.build();
assertEquals(3, eric._properties.length);
+ // test with checking new.json
+ Resource.model('User', {checkNew: true, format: 'json'});
+ var eric = User.build();
+ assertEquals(3, eric._properties.length);
+
// test that specific attributes will override what new returns
var eric = User.build({first_name: "eric", admin: true, daisies: 5});
assertEquals(4, eric._properties.length);

0 comments on commit 0425a9c

Please sign in to comment.