Skip to content

Commit

Permalink
Return undefined for stuff instead of null.
Browse files Browse the repository at this point in the history
  • Loading branch information
benpickles committed Sep 15, 2010
1 parent df12529 commit 57dfd13
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/model_class_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ Model.ClassMethods = {
model = all[i]
if (func.call(model, i)) return model
}

return null
},

each: function(func) {
Expand All @@ -53,11 +51,11 @@ Model.ClassMethods = {
find: function(id) {
return this.detect(function() {
return this.id() == id;
}) || null;
})
},

first: function() {
return this.all()[0] || null;
return this.all()[0]
},

load: function(callback) {
Expand All @@ -78,7 +76,7 @@ Model.ClassMethods = {

last: function() {
var all = this.all();
return all[all.length - 1] || null;
return all[all.length - 1]
},

pluck: function(attribute) {
Expand Down
4 changes: 2 additions & 2 deletions src/model_instance_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Model.InstanceMethods = {
},

id: function() {
return this.attributes.id || null;
return this.attributes.id
},

merge: function(attributes) {
Expand All @@ -87,7 +87,7 @@ Model.InstanceMethods = {
},

newRecord: function() {
return this.id() === null;
return this.id() === undefined
},

reset: function() {
Expand Down
6 changes: 3 additions & 3 deletions src/model_rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Model.REST = function(resource, methods) {
xhr: function(method, url, model, callback) {
var self = this;
var data = jQuery.inArray(method, ["DELETE", "GET"]) > -1 ?
null : this.params(model);
undefined : this.params(model);

return jQuery.ajax({
type: method,
Expand All @@ -88,7 +88,7 @@ Model.REST = function(resource, methods) {
dataType: "json",
data: data,
dataFilter: function(data, type) {
return /\S/.test(data) ? data : null;
return /\S/.test(data) ? data : undefined;
},
complete: function(xhr, textStatus) {
self.xhrComplete(xhr, textStatus, model, callback)
Expand Down Expand Up @@ -140,7 +140,7 @@ Model.REST.parseResponseData = function(xhr) {
try {
return /\S/.test(xhr.responseText) ?
jQuery.parseJSON(xhr.responseText) :
null;
undefined;
} catch(e) {
Model.Log(e);
}
Expand Down
16 changes: 8 additions & 8 deletions test/tests/model_class_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ test("all, count, find, first, add, remove", function() {

same(Post.all(), []);
equals(Post.count(), 0);
ok(Post.find(1) === null);
ok(Post.first() === null);
ok(Post.last() === null);
ok(Post.find(1) === undefined);
ok(Post.first() === undefined);
ok(Post.last() === undefined);

Post.add(post1, post2).add(post3);

Expand All @@ -20,19 +20,19 @@ test("all, count, find, first, add, remove", function() {
equals(Post.find(1), post1);
equals(Post.find(2), post2);
equals(Post.find(3), post3);
equals(Post.find(4), null);
equals(Post.find(4), undefined);
equals(Post.first(), post1);

ok(Post.remove(post2));

same(Post.pluck("id"), [1, 3]);
equals(Post.count(), 2);
equals(Post.find(1), post1);
ok(Post.find(2) === null);
ok(Post.find(2) === undefined);
equals(Post.find(3), post3);
ok(Post.find(4) === null);
ok(Post.find(4) === undefined);

ok(!Post.remove(null));
ok(!Post.remove(undefined));
});

test("maintaining a collection of unique models by object, id and uid", function() {
Expand Down Expand Up @@ -88,7 +88,7 @@ test("detect, select, first, last, count (with chaining)", function() {
ok(Post.detect(function(i) {
indexes.push(i);
return this.attr("title") == "Baz";
}) === null);
}) === undefined);

same(indexes, [0, 1, 2], "should yield index correctly");
indexes = [];
Expand Down
4 changes: 2 additions & 2 deletions test/tests/model_rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test("destroy with named params in resource path", function() {

equals(request.type, "DELETE");
equals(request.url, "/root/3/nested/2/posts/1");
same(request.data, null);
same(request.data, undefined);
});

test("create", function() {
Expand Down Expand Up @@ -280,7 +280,7 @@ test("destroy", function() {

equals(request.type, "DELETE");
equals(request.url, "/posts/1");
same(request.data, null);
same(request.data, undefined);
});

test("destroy failure", function() {
Expand Down

0 comments on commit 57dfd13

Please sign in to comment.