Ember Routes与控制器vs视图 [英] Ember Routes vs Controllers vs Views

查看:75
本文介绍了Ember Routes与控制器vs视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了Ember.js网站上的大部分初学者指南,但我仍然对正确的放置东西感到困惑。

I have read most of the beginner guides on the Ember.js site but I am still confused about the correct place to put stuff.


  1. 路由 - 从在线研究人员建议将路由相关逻辑放在路由中。这一切都很好,但我唯一可以想到的是 this.transisionTo()。我在其他地方阅读所有模型相关操作应该在路由中,因为这是定义模型字段的位置。它是否正确?

  1. Route - from online research people suggested putting routing related logic in the route. That is all good, but the only thing I can think of is this.transisionTo(). I read somewhere else that all model related operations should be in the route because that is where the model field is defined. Is this correct? What are some good use cases of putting actions in the route over the controller?

在 c 视图 - 目前,我看不到视图的点。该文档表示它处理本机DOM事件,但我可能会始终使用将由控制器处理的 {{action}} 帮助器。那么在操作方面使用控制器的视图有什么好用处?一般来说,有一些很好的用例,我将会使用组件来查看可重用的代码。

View - Currently, I can't see the point of the view. The docs say that it handles native DOM events but I will probably always use the {{action}} helper which will be handled by the controller. So what are some good use cases of using the view over the controller in regards to actions? What are some good use cases of the view generally considering I will be using components over views for reusable code.

控制器 - 它对我来说,控制器可以执行View或Route可以执行的任何操作。它可以使用 this.transitionToRoute()转换,它可以使用 this.get('model')保存模型save()并且可以使用 {{action}} 帮助程序来处理操作。所有的初学者教程似乎完全忽略了视图,并使用了控制器。在视图或路线上使用控制器的一些很好的用例是什么?

Controller - It appears to me that the controller can do anything the View or Route can do. It can transition using this.transitionToRoute(), it can save models using this.get('model').save() and can handle actions using the {{action}} helper. All the beginner tutorials seem to ignore the view altogether and use the controller instead. What are some good use cases of using the Controller over the View or Route?

我想这一切都归结为对一切挂在一起的理解差。我找不到任何在线或文档中明确的灰色区域,例如在不同场景中使用 {{action}} 帮助器。

I guess this all comes down to poor understanding of how everything hangs together. I can't find anything online or in the docs that is explicit about the grey areas such as where to use the {{action}} helper for different scenarios. Links to some good materials would be helpful too.

推荐答案

更新:所有这些信息将会对于Ember 2来说是正确的。据我所知,Ember 2.0只会使用组件。

Update: Not all of this information will be correct for Ember 2. As far as I know Ember 2.0 will only use Components.

模型负责与数据存储交互。数据存储的一个示例可以是RESTful服务器或Localstorage。

A Model takes care of interacting with a data store. An example of data store can be a RESTful server or Localstorage.

模板负责构建来自其Controller和View的数据的HTML。控制器可以访问模型。

A Template takes care of building HTML from data available to its Controller and View. A Controller in turn has access to a Model.

控制器和视图非常相似,都对用户生成的事件起作用。控制器应该对诸如 submit 这样的语义事件采取行动,而View应该对交互事件采取行动,如 focus 点击

Controllers and Views are very similar and both act on user generated events. A Controller is supposed to take action on semantic events like submit and a View is supposed to take action on interaction events like focus or click.

Ember has单路由器路由器将URL链接到路由。例如 users /:user_id to users.show route或 UsersShowRoute

Ember has a single Router. Router links urls to Routes. For example users/:user_id to users.show route or UsersShowRoute.

每个路由被激活时,都需要打开一个URL到一个状态。例如 users /:user_id 需要用户登录才能看到朋友的个人资料。路由确保用户已登录并处理登录状态。

Each Route, when activated, takes care of unpacking a url to an state. For example users/:user_id requires a user to be logged in in order to see a friends profile. A Route makes sure a user is logged in and takes care of state of being logged in.

路由从数据存储中获取信息。例如 users / pekhee 将从数据存储中获取 pekhee 用户,并将其提供给Controller和View。

A Route fetches information from data store. For example users/pekhee will fetch pekhee user from a data store and give it to Controller and View.

路由动态选择需要的Controller和View,或者需要多少个。

A Route dynamically chooses what Controller and View it needs or how many of them it needs.

路由管理状态一个应用程序以及该状态应该如何表示。然而,模型管理路由和控制器/视图的管理状态管理它的表示的管理细节。

A Route manages state of an application and how that state should be represented. However Models take care of managing state of application for Route and Controllers/Views take care of managing nitty gritty details of its representation.


  • 如果您的逻辑不关心任何上下文或模型将其放在中激活回调路由。

  • 如果一条逻辑只需要知道模型将它放在路由的后面的回调之后。

  • 如果需要知道Controller的上下文或者与控制器中的资源的语义意义相互作用。

  • 如果需要知道上下文的视图或与资源的表示相互作用的视图将其放在视图中。

  • 如果需要知道上下文或与资源的语义和表现进行交互,首先将事件发送到视图然后将其升级到控制器。

  • If your logic doesn't care about any context or model put it in activate callback of route.
  • If a piece of logic only needs to know model put it in afterModel callback of route.
  • If it needs to know context of Controller or interact with semantic meaning of a resource put it in a controller.
  • If it needs to know context of View or interact with representation of a resource put it in a view.
  • If it needs to know both contexts or interact with both semantic and presentation of a resource first give the event to a view then escalate it to a controller.

当多个控制器与一个模型,在链路上保持最高可能路由的语义动作,否则在大多数本地控制器中保持逻辑。

When more than one Controllers work with one Model, keep semantic actions on highest possible Route in the chain otherwise keep logic in most local Controller.

在视图中保留动画相关代码并使用Liquid Fire。

Keep animation related code in View and use Liquid Fire.

有时每个用户都有很多帖子,每个帖子都有很多评论。尝试让路由返回相关用户。然后向相关用户询问 PostsRou​​te 中的帖子,然后尝试在 CommentsRou​​te 中询问相关评论。这使逻辑尽可能本地化。您的评论路线不需要知道您如何检索用户。

Sometimes each user has a lot of posts and each post has a lot of comments. Try to have a Route return relevant user. Then ask relevant user for posts in PostsRoute and after that try to ask for relevant comments in CommentsRoute. This keeps logic as local as possible. You comments route doesn't need to know how you retrieve a user.

这篇关于Ember Routes与控制器vs视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆