简体   繁体   English

ember.js差异路由文件夹和router.js文件

[英]ember.js difference routes folder and router.js file

I'm trying to make a project in rails and ember.js through the ember-rails gem. 我正在尝试通过ember-rails gem在rails和ember.js中创建一个项目。 I made a rails generate ember:bootstrap and I'm observing the tree it created under assets/javascripts . 我让rails generate ember:bootstrap并且观察它在assets/javascripts下创建的树。

I can't understand what is the difference between things I have to put inside the routes.js file at top level, and in files inside the /routes folder. 我不明白我必须放在顶层的routes.js文件和/routes文件夹中的文件之间的区别是什么。

In the routes.js file at the the top, you will define your routes and resources eg: 在顶部的routes.js文件中,您将定义您的路由和资源,例如:

You can use this rails app as guide. 您可以使用此Rails应用程序作为指南。 The code I use in this answer came from that repo. 我在此答案中使用的代码来自该回购。

EmBlog.Router.map(function() {
  this.resource("posts", {path: '/posts'}, function(){
    this.route('new');
    this.route('show', {path: '/:post_id'}) ;
    this.route('edit', {path: '/:post_id/edit'});

 });
});

If you want to customize any of those routes or resources , then you create a new file in /routes folder . 如果要自定义这些路由或资源中的任何一个 ,则可以在/ routes文件夹中创建一个新文件 For example if we want to customize the show route by defining an event to delete a post in that router and to also customize what data is returned using the model hook , then we would need to create a file in / routes / posts / show_route.js and add our code there: 例如,如果我们要通过定义一个事件来自定义 显示路由以删除该路由器中的帖子 ,并还自定义使用模型钩子 返回的数据 ,那么我们需要在/ routes / posts / show_route中创建一个文件。 js并在其中添加我们的代码:

EmBlog.PostsShowRoute = Ember.Route.extend({

 model: function(params) {
  return EmBlog.Post.find(params.post_id);
 },

 setupController: function(controller, model){
  controller.set('content', model);
 },

 events: {
  destroyPost: function(post) {
   post.deleteRecord();
   post.get('store').commit();
   this.transitionTo('posts');
  }
 }

});

The idea of splitting the route is to ensure it doesn't get too big or filled with 100's of lines of code. 分割路径的想法是确保路径不会太大或不会充满100行代码。 If you prefer defining both the top level routes and their customization in thesame file. 如果您希望同时在同一文件中定义顶级路由及其自定义。 You can. 您可以。 Here is another rails app that does just that. 这是另一个实现此目的的Rails应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM