Rails Presenters 文件夹有什么用?
What is the Rails Presenters folder for?
Rails Presenters 文件夹有什么用?这个文件夹里有什么?为什么需要这个文件夹?
presenters
是一种设计模式,通常称为 Model View Presenter(MVP)
这是模型视图控制器模式的派生,用于创建用户界面。
它对于使代码更干的关注点分离很有用。
维基百科是这样描述的
model - interface defining the data to be displayed or otherwise acted upon in the user interface.
presenter - acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.
view - a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter
在Rails
Ruby中的演示者
Presenter 很简单 类,位于模型和视图之间,提供了一种很好的、DRY 面向对象的方式来处理复杂的显示逻辑。
在 Rails 中,约定是它们位于 app/presenters
文件夹中
这是一篇有用的文章,解释了 Rails 上的 Ruby 中的模式及其用法。
https://kpumuk.info/ruby-on-rails/simplifying-your-ruby-on-rails-code/
Presenters 文件夹是您的 Presenter 代码所在的位置......我知道,很明显,我会解释......我认为 Presenters 和 Decorators 的方式是作为模型的抽象,以便按摩在将数据属性提供给视图之前。如果您熟悉 helpers,那么,Presenters 有点像 helpers,因为它们正在为视图准备一些数据,除了 helpers 通常用作所述视图的实用方法,而 presenters 更多是关于呈现实际属性。
这个link很好地解释了区别:https://awead.github.io/2016/03/08/presenters/
希望对您有所帮助。
演示者整理您的观点
当人们在 rails 上下文中提到演示者时(与模型-视图-演示者、MVC、MVVM 讨论等的讨论相反),他们指的是在您的视图中事情看起来非常复杂的情况:到处都是 if
语句,很难通读一遍。
或者打个日常比喻:想象一下你有一个非常凌乱的房子,到处都是东西堆放 - 以至于很难走过去。您可以将演示器想象成一个大车库,您可以在其中整齐地组织一切。这样可以更轻松地穿过您的房子,并且您仍然拥有所需的所有用具。
回到 rails 上下文:Presenters 允许您将所有复杂的逻辑删除到其他地方:到 Presenter 的文件夹中,这样当您阅读您的视图时,从中可以更容易理解更高的水平。杂乱不存在,复杂性不存在:已转移到其他地方。如果您需要更多详细信息,则必须转到相关文件夹。逻辑不需要包含在名为 "Presenters" 的文件夹中,但可以按照惯例将其放在该文件夹中。
Rails Presenters 文件夹有什么用?这个文件夹里有什么?为什么需要这个文件夹?
presenters
是一种设计模式,通常称为 Model View Presenter(MVP)
这是模型视图控制器模式的派生,用于创建用户界面。
它对于使代码更干的关注点分离很有用。
维基百科是这样描述的
model - interface defining the data to be displayed or otherwise acted upon in the user interface.
presenter - acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.
view - a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter
在Rails
Ruby中的演示者Presenter 很简单 类,位于模型和视图之间,提供了一种很好的、DRY 面向对象的方式来处理复杂的显示逻辑。
在 Rails 中,约定是它们位于 app/presenters
文件夹中
这是一篇有用的文章,解释了 Rails 上的 Ruby 中的模式及其用法。
https://kpumuk.info/ruby-on-rails/simplifying-your-ruby-on-rails-code/
Presenters 文件夹是您的 Presenter 代码所在的位置......我知道,很明显,我会解释......我认为 Presenters 和 Decorators 的方式是作为模型的抽象,以便按摩在将数据属性提供给视图之前。如果您熟悉 helpers,那么,Presenters 有点像 helpers,因为它们正在为视图准备一些数据,除了 helpers 通常用作所述视图的实用方法,而 presenters 更多是关于呈现实际属性。
这个link很好地解释了区别:https://awead.github.io/2016/03/08/presenters/
希望对您有所帮助。
演示者整理您的观点
当人们在 rails 上下文中提到演示者时(与模型-视图-演示者、MVC、MVVM 讨论等的讨论相反),他们指的是在您的视图中事情看起来非常复杂的情况:到处都是 if
语句,很难通读一遍。
或者打个日常比喻:想象一下你有一个非常凌乱的房子,到处都是东西堆放 - 以至于很难走过去。您可以将演示器想象成一个大车库,您可以在其中整齐地组织一切。这样可以更轻松地穿过您的房子,并且您仍然拥有所需的所有用具。
回到 rails 上下文:Presenters 允许您将所有复杂的逻辑删除到其他地方:到 Presenter 的文件夹中,这样当您阅读您的视图时,从中可以更容易理解更高的水平。杂乱不存在,复杂性不存在:已转移到其他地方。如果您需要更多详细信息,则必须转到相关文件夹。逻辑不需要包含在名为 "Presenters" 的文件夹中,但可以按照惯例将其放在该文件夹中。