2013-10-16 54 views
0

我想了解CanJS的路由。到目前爲止,我建立了以下路線。can.routing:觸發從#!/ foo/bar到#!/ foo的更改

can.route('plant/:plant/', { 
    plant : undefined, 
    day : undefined 
}); 
can.route('plant/:plant/day/:day', { 
    plant : undefined, 
    day : undefined 
}); 

我還沒有設置聽衆,因爲我只是在控制檯中嘗試了這一點。以下工作正常:

can.route.attr({plant : 1}) // ==> #!plant/1/ 
can.route.attr({plant : 1, day : 3}) // ==> #!plant/1/day/3 

但我已經這樣做了之後,我想觸發一個事件去「上」層次結構中,回#/工廠/ 1級!我試着做can.route.attr({plant : 1, day : undefined}),但那沒有做任何事情。 can.route.attr({plant : 1, day : null})剛剛導致#!plant/1/day/null

那麼,我該如何「重置」路線以「知道」任何關於哪一天?

回答

0

當我得知can.route基本上是所謂的Observable我明白我實際上想要做的是去除一個屬性。要做到這一點,所有人必須做的是

can.route.removeAttr('day') // ==> #!plant/1/ 
0

我遇到了同樣的問題,並希望在此處記錄這一點。更好的實現方法是使用can.route.url方法生成適當的路由URL。不幸的是canjs沒有實現routeTo方法,這將抽象掉下面的代碼(也可以決定是否要使用時,使用pushState的...):

can.route('plant/:plant'); 
can.route('plant/:plant/day/:day'); 

window.location.hash = can.route.url({plant: 1}); // #!plant/1 
window.location.hash = can.route.url({plant: 1, day: 2}); // #!plant/1/day/2 

理想的情況下,我們應該能夠做到像這個:

can.routeTo({plant: 1}); 
// #!plant/1  (without pushstate) 
// [root]/plant/1 (with pushstate) 

注意:我將在接下來的幾周內建立這個,並可能發送一個拉請求。