ember.js - EmberJS dynamic routing and configuration -
i use emberjs v1.0.0-rc.2 , requirejs.
app structure kinda that.
- app - - modulea
my main file:
#app/main.js var app = ember.application.create({ version: '1.0', //log router transitions: log_transitions: true }); app.router.map(function() { approuting.call(this); }); app = approutingextend(app); app.initialize();
functions approuting() , approutingextend() find in fellow file:
#app/routing.js function approuting() { this.resource('root', {path: '/'}, function() { this.resource('modulea', {path: '/'}, function() { modulearouting.call(this); }); }); } function approutingextend(app) { var modulearouting = modulearoutingextend(); //check if modulearouting not empty if (!ember.isempty(modulearouting)) { $.each(modulearouting, function(key, value) { //check if key string of letters // , if value ember.route.extend({}) if (typeof key === 'string' && /^[a-za-z]+$/.test(key) && value.tostring() == '(subclass of ember.route)') { eval("app.root" + "modulea" + key + "route = value"); } else { //throw error } }); } return app; }
functions modulearouting() & modulearoutingextend() find in follow file:
#app/modulea/routing.js function modulearouting() { this.route("contributors", {path: "/"}); this.resource('acontributor', {path: "/:githubusername"}, function() { this.route("details", {path: "/"}); this.route("repos", {path: "/repos"}); }); } function modulearoutingextend() { var routes = {}; routes['contributors'] = ember.route.extend({ /* code */ }); routes['acontributor'] = ember.route.extend({ /* code */ }); routes['acontributordetails'] = ember.route.extend({ /* code */ }); routes['acontributorrepos'] = ember.route.extend({ /* code */ }); return routes; }
i created approuting()
, modulearouting()
able add dynamically routing path application, adding new module or remove one. way, each module have intern structure , approuting()
merge them.
however, i'm not sure modulearoutingextend()
, more approutingextend()
. in last one, try modify routes app.rootmoduleacontributorsroute
. way don't have information directly routes have been created modulearouting.call(this)
, cannot know name of variable rootmoduleacontributorsroute
. reason use eval dynamic getting 'contributors' modulearoutingextend()
, value, ember.route.extend({/* code *});
so, question is: there better way add dynamically routes application , configuration? , if not, still way?
Comments
Post a Comment