Permalink
Browse files

Merge branch 'master' of git://github.com/thoughtbot/jester

  • Loading branch information...
2 parents 01dfd6a + e28ba9f commit aa751bee8637de35a3d82029736343e9222409c3 Nat Budin committed May 1, 2008
Showing with 43 additions and 23 deletions.
  1. +1 −0 .gitignore
  2. +24 −18 jester.js
  3. +18 −5 test/jester_test.html
View
@@ -0,0 +1 @@
+prototype*.js
View
@@ -1,8 +1,7 @@
// Jester version 1.5
// Released October 25th, 2007
-// Homepage: http://www.jesterjs.org
-// Compatible and tested with Prototype 1.5.1.1
+// Compatible, tested with Prototype 1.6.0.2
// Copyright 2007, thoughtbot, inc.
// Released under the MIT License.
@@ -76,21 +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) {
@@ -260,15 +266,16 @@ Object.extend(Jester.Resource, {
},
_interpolate: function(string, params) {
+ if (!params) return string;
+
var result = string;
- for(var val in params) {
- var re = new RegExp(":" + val, "g");
- if(result.match(re))
- {
- result = result.replace(re, params[val]);
- delete params[val];
+ params.each(function(pair) {
+ var re = new RegExp(":" + pair.key, "g");
+ if (result.match(re)) {
+ result = result.replace(re, pair.value);
+ params.unset(pair.key);
}
- }
+ });
return result;
},
@@ -512,7 +519,7 @@ Object.extend(Jester.Resource.prototype, {
this._setErrors(errors);
else {
var attributes;
- if (this._format == "json") {
+ if (this.klass._format == "json") {
attributes = this._attributesFromJSON(transport.responseText);
}
else {
@@ -606,9 +613,8 @@ Object.extend(Jester.Resource.prototype, {
},
_errorsFrom : function(raw) {
- if (this._format == "json") {
+ if (this.klass._format == "json")
return this._errorsFromJSON(raw);
- }
else
return this._errorsFromXML(raw);
},
@@ -633,7 +639,7 @@ Object.extend(Jester.Resource.prototype, {
if (!xml) return false;
var doc = Jester.Tree.parseXML(xml);
- if (doc.errors) {
+ if (doc && doc.errors) {
var errors = [];
if (typeof(doc.errors.error) == "string")
doc.errors.error = [doc.errors.error];
View
@@ -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);
@@ -620,16 +627,17 @@
function testErrorsFromJSON() {
var errorJSON = '[["name", "can\'t be blank"], ["city", "can\'t be blank"]]';
- var eric = new User();
- var errors = eric._errorsFromJSON(errorJSON);
+ Resource.model('User', {format: 'json'});
+ var eric = User.build();
+ var errors = eric._errorsFrom(errorJSON);
assertEquals(2, errors.length);
assertEquals("Name can't be blank", errors[0]);
assertEquals("City can't be blank", errors[1]);
- assertEquals(false, eric._errorsFromJSON(' '));
- assertEquals(false, eric._errorsFromJSON(''));
- assertEquals(false, eric._errorsFromJSON(null));
+ assertEquals(false, eric._errorsFrom(' '));
+ assertEquals(false, eric._errorsFrom(''));
+ assertEquals(false, eric._errorsFrom(null));
}
function testErrorsFromXML() {
@@ -649,6 +657,11 @@
/********************************************
************ INTERNAL TESTS ****************
********************************************/
+
+ function testGlobalScope() {
+ // IE7 compatibility
+ assertEquals("IE7 incompatibility", User, window.User);
+ }
function testSetProperties() {
var eric = User.build()

0 comments on commit aa751be

Please sign in to comment.