我应该在视图中避免多少计算? [英] How much should I avoid computations in my views?

查看:64
本文介绍了我应该在视图中避免多少计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中n个用户可以在公共场所互相交谈(例如消息传递应用程序).因为您可能希望每次演讲都使用不同的简历(例如,关于Ruby on Rails的讨论与关于心理学的讨论需要不同的简历),所以我有一个Spkr模型,其中包含一个用户和一个Tlk.以下代码成功表示,在用户个人资料页面上,对于每个实例都是Spkr,Tlk,并且每个Spkr的图像都是可见的参与者(因此,如果一个Tlk有三个参与者,则所有三个图像都是可见的)

I am building an application where n users can talk to each other (like a messaging application) in public. Because you might want to have a different bio for each talk you do (for example a discussion about me regarding Ruby on Rails would need a different bio than one about Psychology) I have a Spkr model which has a User and a Tlk. The below code successfully means that on the users profile page, for each instance of them being a Spkr, the Tlk, and it's participants is visible with each Spkr's image (so if a Tlk has three participants, then all three images will be visible).

设置是这样的,默认图像是用户的图像,但是Spkr也可以通过将一个图像上传为Spkr来自定义其图像.我担心会给前端加载过多的计算量.现在一切正常...可以吗?还是应该限制构建视图时发生的计算?

The setup is such where the default image is the User's image, but the Spkr can also customise their image by uploading one as a Spkr. I am worried that I am loading the front end with too much computation. Right now everything works... so is it ok? Or should I be limiting the computation happening when building views?

谢谢

<% @user.spkrs.each do |spkr| %>
  <%= link_to show_tlk_path(spkr.tlk) do %>
    <h4><%= spkr.tlk.title %></h4>
    <% spkr.tlk.spkrs.each do |speaker| %>
      <div class="tlk-tlking-image spkr-image image-spkr-<%= spkr.id %>"
        <% if speaker.image.present? %>
          style="background-image: url(<%= rails_blob_url(speaker.image) %>)"
        <% elsif speaker.user.image.present? %>
          style="background-image: url(<%= rails_blob_url(speaker.user.image) %>)"
        <% end %>
        >
      </div>
      <p><%= speaker.name %></p>
    <% end %>
  <% end %>
<% end %>

推荐答案

倾向于将视图尽可能不包含后端"计算作为良好实践.这些文件通常由前端开发人员处理,他们甚至可能不知道如何编写ruby代码,因此视图中的代码越少越好.在Rail的Model Controller View框架中,它也不是应有的.

It tends to be considered good practice to keep the view as free of 'back end' calculations as possible. These files are often worked on by front end developers who may not even know how to code ruby, so the less of it that is in the view the better. It's also just not where it belongs in rail's Model Controller View framework.

首先,您输入的代码可以简化为:

First of all the code you've put can be simplified to:

<% @user.spkrs.each do |spkr| %>
  <%= link_to show_tlk_path(spkr.tlk) do %>
    <h4><%= spkr.tlk.title %></h4>
    <% spkr.tlk.spkrs.each do |speaker| %>
      <div class="tlk-tlking-image spkr-image image-spkr-<%= spkr.id %>"
        style="background-image: url(<%= rails_blob_url((speaker.image  || speaker.user.image) %>)"
        >
      </div>
      <p><%= speaker.name %></p>
    <% end %>
  <% end %>
<% end %>

但是正如您所说的,如果您想在更合适的地方进行处理,我会在Speaker类中添加一个方法:

But as you say, if you want to handle this in a more appropriate place, I'd add a method to the Speaker class:

# app/models/speaker.rb
class Speaker << ApplicationBase
  def image_for_view
    image || user.image
  end
end

这将使您可以调用speaker.image_for_view,我认为它在视图文件本身中可以很好地读取.

This will let you call speaker.image_for_view which I think reads nicely in the view file itself.

这篇关于我应该在视图中避免多少计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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