Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug where hydrate was not cleaning up special properties when in an array #6

Merged
merged 2 commits into from
Apr 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spec/HydrateSpec.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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