Skip to content

Commit

Permalink
Merge pull request #6 from freethenation/master
Browse files Browse the repository at this point in the history
Fixed bug where hydrate was not cleaning up special properties when in an array
  • Loading branch information
nanodeath committed Apr 28, 2013
2 parents 2845c04 + dbad0c0 commit 28560ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 9 additions & 0 deletions spec/HydrateSpec.js
Expand Up @@ -28,6 +28,15 @@ describe("Hydrate", function() {
}
extend(BasicSubclass, BasicClass);

it("should cleanup hydrate properties", function() {
var input = {}
var basic = {f:[{a:{}}]}
input.objArray = [{}, basic, {}];
input.basic = basic;
input.objHash = {a: {}, b: basic, c: {}};
expect(hydrate.parse(hydrate.stringify(input))).toEqual(input);
});

it("should serialize primitives", function() {
var inputs = [undefined, null, 3, "foo", ["a", 3, "bar"], true, false]
for(var i = 0; i < inputs.length; i++){
Expand Down
15 changes: 11 additions & 4 deletions src/hydrate.coffee
Expand Up @@ -245,6 +245,10 @@ scope = this
# Also, runs migrations.
# Private.
clean: (o, cleaned=[]) ->
# if its not an object then there is no cleaning to do
if o == null || typeof o != "object" then return true
# if we have already cleaned this object then return
if !Util.isArray(o) && cleaned.indexOf(o) > -1 then return true
# migrate
migrations = @migrations[o.__hydrate_cons]
if(o.version? &&
Expand All @@ -253,15 +257,18 @@ scope = this
for num in [o.version..migrations.length - 1]
migrations[num].call(o)
delete o.version

# do actual clean
cleaned.push o
if typeof o == "object" && !Util.isArray(o)
if Util.isArray(o)
for i in o
@clean(i, cleaned)
else # o is an object
for k, v of o
if k == "__hydrate_id" || k == "__hydrate_cons"
delete o[k]
else if typeof v == "object" && v && !Util.isArray(o) && cleaned.indexOf(v) < 0
else
@clean(v, cleaned)
true
return true

# Declare a migration for an object -- this will automatically run callbacks
# on old objects to get them in sync with current Javascript classes.
Expand Down

0 comments on commit 28560ed

Please sign in to comment.