Skip to content

Commit

Permalink
Fix #1420, deep cleans routes as opposed to just the first level.
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoshdean committed Feb 2, 2015
1 parent d0c4338 commit c946de2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
19 changes: 13 additions & 6 deletions route/route.js
Expand Up @@ -646,22 +646,29 @@ steal('can/util', 'can/map', 'can/list','can/util/string/deparam', function (can
var hash = can.route._call("matchingPartOfURL");
var oldParams = curParams;
curParams = can.route.deparam(hash);

// if the hash data is currently changing, or
// the hash is what we set it to anyway, do NOT change the hash
if (!changingData || hash !== lastHash) {
can.batch.start();
for(var attr in oldParams){
if(!curParams[attr]){
can.route.removeAttr(attr);
}
}
recursiveClean(oldParams, curParams, can.route.data);

can.route.attr(curParams);
// trigger a url change so its possible to live-bind on url-based changes
can.batch.trigger(eventsObject,"__url",[hash, lastHash]);
can.batch.stop();
}
};

var recursiveClean = function(old, cur, data){
for(var attr in old){
if(!cur[attr]){
data.removeAttr(attr);
}
else if(Object.prototype.toString.call(old[attr]) === "[object Object]") {
recursiveClean( old[attr], cur[attr], data.attr(attr) );
}
}
};

return can.route;
});
34 changes: 34 additions & 0 deletions route/route_test.js
Expand Up @@ -692,6 +692,40 @@ steal("can/route", "can/test", function () {
},1);
});
});

test("routes should deep clean", function() {
expect(2);
setupRouteTest(function (iframe, iCanRoute, loc) {
iCanRoute.ready();
var hash1 = can.route.url({
panelA: {
name: "fruit",
id: 15,
show: true
}
});
var hash2 = can.route.url({
panelA: {
name: "fruit",
id: 20,
read: false
}
});


loc.hash = hash1;

loc.hash = hash2;

setTimeout(function() {
equal(iCanRoute.attr("panelA.id"), 20, "id should change");
equal(iCanRoute.attr("panelA.show"), undefined, "show should be removed");

teardownRouteTest();
}, 30);

});
});
}

test("escaping periods", function () {
Expand Down

0 comments on commit c946de2

Please sign in to comment.