2

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.

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

1

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
}
YOU
  • 120,166
  • 34
  • 186
  • 219
  • 1
    no idea about emberjs, my next target to learn. but I got my work done with angularjs for now. – YOU Jan 03 '14 at 15:11
  • 1
    @lauthiamkok I think this discourse source file says similar can done in ember too. https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/views/user_selector_view.js – YOU Jan 03 '14 at 15:21
1

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);
  }
});
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • thank you. it gives me some hope to use emberjs as I don't like angularjs while backbonejs does not really suit my app... :) – Run Jan 03 '14 at 15:41