我正在努力寻找任何好的 Ember.js 路由示例。
我看到有一个路由集合作为 State 对象的一部分,但我找不到任何如何使用它的示例。
路由最近在核心 Ember.js 中可用,请参阅 Tom Dale 的博客文章。
核心开发人员 Yehuda Katz 写了一篇关于新路由功能使用的要点。这是一本很好的读物,除了路由之外,还说明了它如何与控制器集成。
为了得到基本的想法,这里有一个取自要点的代码示例:
App.Router = Ember.Router.extend({
root: Ember.State.extend({
index: Ember.State.extend({
route: '/',
redirectsTo: 'calendar.index'
}),
calendar: Ember.State.extend({
route: '/calendar',
index: Ember.State.extend({
route: '/'
}),
preferences: Ember.State.extend({
route: '/preferences'
})
}),
mail: Ember.State.extend({
route: '/mail',
index: Ember.State.extend({
route: '/'
}),
preferences: Ember.State.extend({
route: '/preferences'
})
})
})
});
// If the user navigates to the page with the URL
// www.myapp.com/, you will start in the root.calendar.index state.
// The redirection to the calendar.index state will cause the URL
// to be updated to www.myapp.com/calendar
router.transitionTo('preferences');
// URL => www.myapp.com/calendar/preferences
router.transitionTo('mail.preferences');
// URL => www.myapp.com/mail/preferences
router.transitionTo('index');
// URL => www.myapp.com/mail
我强烈推荐 ember 网站上的本指南:http: //emberjs.com/guides/router_primer/
我相信这个内容很新——几周前我第一次开始尝试理解 Ember 路由时,我当然没有注意到它。
我在我的应用程序中使用sproutcore-routing,因为:
有关文档,请查看spoutcore-routing 测试
Ember routing changed entirely over Christmas. They posted some new documentation to help, but removed all the old tutorials. I'm guessing the old ways of doing things (in the previous answers) will be deprecated. http://emberjs.com/guides/routing/
编辑:由于 Ember.js 路由器的重大变化,这个答案在 1.0 PRE-RELEASE 左右就已经过时了。ember.js 的现代版本应该使用标准路由指南
我想这是(至少现在)非常个人化的。我喜欢 ghempton 的 ember-routemanager。如果您需要一些帮助,我可以帮助您。连同他的 ember-layout 包,它们一起工作得很好。
http://codebrief.com/2012/02/anatomy-of-a-complex-ember-js-app-part-i-states-and-routes/