문제

This is how I do my routes in backbonejs where the routing and its params are obtained first before deciding which external template to call. I find this is quite flexible.

var Router = Backbone.Router.extend({
        routes: {
            //'':                                                         'renderBasic',
            ':module/:method/':                                        'renderDynamicViewPageBasic',
            ':module/:branch/:method/':                                'renderDynamicViewPageBranch',
            ':module/:branch/:method/set:setnumber/page:pagenumber/':  'renderDynamicViewPagePager',
            ':module/:branch/:method?set=:setnumber&page=:pagenumber': 'renderDynamicViewPagePager'
        },

        renderDynamicViewPageBasic: function (module,method) {

            $(el).html(Handlebars.getTemplate('template1')(data)); 
        },

        renderDynamicViewPageBranch: function (module,branch,method) {

            $(el).html(Handlebars.getTemplate('template2')(data)); 
        },

        renderDynamicViewPagePager: function (module,branch,method,setnumber,pagenumber) {

            $(el).html(Handlebars.getTemplate('template3')(data)); 

        }
    });

How about in emberjs, can I do the same - do the rout and get its params afirst before deciding which external template to call?

I read the documentation and tested it. It seems to be less flexible - for instance,

App.Router.map(function() {
  this.route("about", { path: "/about" });
  this.route("favorites", { path: "/favs" });
});

Is it possible to get the route and params and then the controller before getting the template?

if not, it seems to be the same as case using Angularjs which I finally decided not to use it because it gets the template first before sorting out the params.

도움이 되었습니까?

해결책

You can define the template "post params" in EmberJs using the renderTemplate hook, where you can customize which template you'd like to use.

http://emberjs.jsbin.com/oXUqUJAh/1/edit

App.Router.map(function() {
  this.route('apple', {path: 'apple/:id'}); 
});

App.AppleRoute = Ember.Route.extend({
  model: function(params) {
    return {coolProperty: params.id};
  },
  renderTemplate: function(controller, model) {
    // send in the template name
    this.render(model.coolProperty);
  }
});

다른 팁

You can pass a function together with $route params to get customized result in angularjs actually.

template: function($params) {
  return app.$templateCache.get($params); // or make template yourself from another source
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top