分享 10 rules for Rails view

loveky · 2013年07月16日 · 最后由 loveky 回复于 2013年07月17日 · 2635 次阅读

RULES FOR THE VIEW

  1. Our markup should have meaning. We write templates using semantic HTML.

  2. Our style sheets should handle presentation. We don’t use markup to style or use images when CSS will do.

  3. Our templates should be free of client-side code. We unobtrusively attach behavior from our JavaScript files.

  4. Our templates should be easy to read. We consistently indent correctly using spaces instead of tabs, type lines no longer than eighty characters, and extract complex logic to helpers and presenters.

  5. Our templates should be easy to find. We use standard naming conventions and place them in the directory for the related resource (or the layout).

  6. Our markup should be easy for the entire team to modify. We prefer rendering partials over generating markup from Ruby code.

  7. Our technology choices should help, not hinder, the team. We use the templating language and tools that work best for all of us.

  8. Our designs for the Web should work on a variety of devices and browsers. We build for the simplest interactions first and support progressive enhancement.

  9. Our designs for email must work for a wide range of providers. We use HTML tables and images as necessary and always provide a plain-text alternative.

  10. Our application should perform as well as it needs to, when it needs to. We implement the most elegant approach first, then we optimize when necessary.

楼下翻译?❤ : 💔

#1 楼 @Peter 谢谢,我在平板上发的帖,没办法复制文本,只能贴地址了

Use no way as way. Having no limitation as limitation.            ーー Bruce on Rails

#1 楼 @Peter 本来想翻译来着,但是发现都是浅显易懂的。

PS: 第 3 条作为建议还可以,有的时候真的无法做到完全 HTML 与 JavaScript 分开写啊。

#5 楼 @cisolarix 我的方法是写一个 helper,在 html 里面用这个 helper 来执行 js 代码:run :init_some_page

#5 楼 @cisolarix 有的时候真的无法做到完全 HTML 与 JavaScript 分开写啊。 啥情形呢?

#6 楼 @linjunhalida 可否举个小例子?

#7 楼 @loveky 比如一个 ajax form,ajax 请求 /ideas/new,要返回一个 form html,同时跑一些 js,比如 form 完成后异步发送之类。传统的写法是在_new.html.haml 里面写上 js,我的方法就是加上一个<input type='hidden' data-function='init_idea_new' />,这个可以用 helper 生成。

然后在 ajax 请求结束的时候搜索一遍收到的 html,发现如果有input[data-function],就执行init_idea_new这个 js 函数就好了。

#8 楼 @linjunhalida 如果只是 init_idea_new 里的 js 只是绑定 form 提交时 js code,也可以通过 jquery 里的 on 函数定义吧

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册