Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

ember.js - Ember router: how to use transitionTo

I have a link that looks like this

index.html#/calendar/year/month

This is how I set up my routes:

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);
  }
});

I can get to it by doing this:

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;

But I'd like to use the router instead.

I tried:

var router = this.get('target');
router.transitionTo('#calendar/'+year + '/' + nextMonth);

But I get this error:

Uncaught Error: assertion failed: The route #calendar/2013/5 was not found

I also tried:

var router = this.get('target');
router.transitionTo('calendar/'+year + '/' + nextMonth);

But this also gives me an error:

Uncaught Error: assertion failed: The route calendar/2013/5 was not found

Edit: displaying my routing above

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Oposite from what I said in the comments, this can actually be done without the need of nested routes, using the Route#serialize.

I've made this fiddle (demo here) with a scenario similar to what you described:

In the application, I'm storing the month and year arguments

window.App = Ember.Application.create({
    title: 'Cool App',
    calendar: { 
        month: new Date().getMonth()+1, 
        year: new Date().getFullYear() 
    }
});

Defined the routes

App.Router.map(function() {
    this.route("home");
    this.resource('calendar', { path: 'calendar/:year/:month'});
});

In the calendar route, I've added the serialize method, to translate the properties in obj to the app, then I connected with 3rd party lib in setupController to get the days property and set its content.

App.CalendarRoute = Em.Route.extend({
    activate: function() {
        $(document).attr('title','Events')
    },
    serialize: function(obj) {
        return {
            year: obj.year, month: obj.month
        }
    },
    setupController: function(controller, model) {
        var obj = {
            days: calendar.getDaysInMonth(model.month, model.year),
            year: model.year,
            month: model.month
        };
        controller.set('content', obj);
    }
});

In Handlebars, using a {{linkTo}} helper, I am passing the calendar property defined in my App class as the argument:

{{#linkTo calendar App.calendar tagName="li"}}
    <a {{bindAttr href="view.href"}}>
        Calendar
    </a>
{{/linkTo}}

This will generate a link to ~/#/calendar/2013/4


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...