我有一個鏈接,看起來像這樣灰燼路由器:如何使用transitionTo
index.html#/calendar/year/month
這是我建立了我的路線:
App.Router.map(function() {
this.resource('calendar', {path: 'calendar/:currentYear/:currentMonth'});
});
App.CalendarRoute = Ember.Route.extend({
model: function (params) {
var obj = {
weeks: calendar.getDaysInMonth(params.currentMonth, params.currentYear),
currentMonth: params.currentMonth,
currentYear: params.currentYear
};
return obj;
},
setUpController: function(controller, model) {
controller.set('content', model);
}
});
我可以這樣得到它:
var currentMonth = this.get('content.currentMonth');
var nextMonth = parseInt(currentMonth)+1;
var route = '#/calendar/'
var year = this.get('content.currentYear');
window.location.href= route + year + '/' + nextMonth;
但我想用路由器代替。
我想:
var router = this.get('target');
router.transitionTo('#calendar/'+year + '/' + nextMonth);
但我得到這個錯誤:
Uncaught Error: assertion failed: The route #calendar/2013/5 was not found
我也試過:
var router = this.get('target');
router.transitionTo('calendar/'+year + '/' + nextMonth);
但是,這也給了我一個錯誤:
Uncaught Error: assertion failed: The route calendar/2013/5 was not found
編輯:顯示我上面的路由
嗯,在''router.transitionTo('#calendar /'+ year +'/'+ nextMonth);'你在#之後缺少一個「/」。我不知道還有什麼可能是錯誤的,但我通常使用'this.transitionToRoute('example.name');'或'App.Router.router.transitionTo('example.name');''。 – 2013-04-10 12:43:48
聽起來不錯,所以我只是試過:router.transitionTo('#/ calendar /'+ year +'/'+ nextMonth);但後來我得到未捕獲的錯誤:斷言失敗:沒有找到路線#/ calendar/2013/5 – redconservatory 2013-04-10 12:52:15
@redconservatory我相信這個問題與您的路線定義有關。這可能應該是一個嵌套路線,其中一個分段爲一年,另一個分段爲一個月份。此外,他目前正在做的方式似乎在該路線中缺少'model'方法中的'params'參數。 – MilkyWayJoe 2013-04-10 13:36:20