使用rails和backbone.js让我的应用程序更加干燥

发布于 2025-01-07 15:48:05 字数 248 浏览 3 评论 0原文

好吧,所以我决定在我的 Rails 应用程序中使用backbone.js,但我觉得我做了很多冗余编码。我的主干中有很多模型和模板,它们或多或少与 Rails 等效项相同。有某种宝石可以处理这个问题吗?类似于 act_as_backbonemodel 的东西,您可以将其放入生成主干模型的 Rails 模型中(可以覆盖以添加功能)。另外,如果存在某种rails-backbone-view gem,可以让您将模板放在视图目录中并使用正常的部分/重用正常的rails 视图,那就太好了。

Okey, so I decided to go with backbone.js in my rails app, but I feel that I'm doing a lot of redundant coding. I'm having a lot of models and templates in backbone that are more or less the same as their rails equivalent. Is there some kind of gem that handles this? Something like act_as_backbonemodel that you put in to your rails model that generates the backbone-model (with possibility to be overridden to add functionality). Also it would be great if there existed some kind of rails-backbone-view gem that lets you put your templates in your views directory and use normal partials/reuse normal rails views.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌吟 2025-01-14 15:48:05

我知道的最接近的 Gem 是: https://github.com/codebrew/backbone-rails
也许与以下组合结合: https://github.com/netzpirat/haml_coffee_assets

在 app/ 中拥有 Backbone 模板视图没有意义,因为这些应该是精益的 JS(或编译为 js)文件,其中(几乎)不包含任何逻辑。您应该考虑通过资产管道从 CDN 加载它们。

一个常见的用例是让 Backbone 处理前端数据逻辑并使用 Rails 作为 API。

The closest Gem I know of is: https://github.com/codebrew/backbone-rails
Perhaps in combination with: https://github.com/netzpirat/haml_coffee_assets

Having the Backbone templates in app/views doesn't make sense, since these should be lean JS (or compiled to js) files that contain (almost) no logic. You should consider loading them from a CDN through the asset pipeline.

A common use case is to let Backbone handle the front data logic and use Rails as just your API.

苍暮颜 2025-01-14 15:48:05

当我开始使用 Backbone 和 Rails 时(大约一年前),我也想要一个像这样的 gem(当时我使用backbone-rails)。最初,骨干网和 Rails 之间似乎有很多东西是重复的,但随着开发的进展,我发现这是例外而不是规则。特别是如果您使用 Rails 作为 API,这种 gem 可能只在项目开始时有帮助,之后就不太相关了。

我这样说是因为我们的大多数模型不再与 Rails 资源一一对应。例如,虽然需要有关人员的信息,但该信息来自多种资源的组合,并且排除了主要人员记录中的大部分数据。此外,创建和修改它们在我们的应用程序中并不合适,并且创建只能通过邀请进行,并且完全作为不同的资源公开。

不需要其他资源,但其他资源具有如此小的功能或数据子集,或者具有来自多个资源的数据,因此让某些东西生成它们实际上是行不通的。

由于rails 是我们案例中的API,因此我们在视图层中没有任何重复。

我发现创建一个与您使用主干的方式相匹配的自定义生成器,或者在您选择的编辑器中使用一些模板是更灵活的解决方案。

在组织方面,我们在app/assets/javascripts目录下添加了backbone作为目录,并在这里为模型、视图、路由器、助手等创建了目录。这允许使用 CoffeeScript 来编写主干,而不需要任何 gem。我们使用 jasmine 和 jasminerice gems 来测试我们的主干代码,其中包括用于固定装置的 Coffeescript 和 haml(这些位于 spec/javascripts 目录中}。

至于模板,它们都是控制器中渲染到标头中的 Rails 视图部分。 Backbone 通过 id 查找它们并从那里使用它们

希望这会有所帮助。

When I started using Backbone with Rails (almost a year ago), I also wanted a gem like this (I used backbone-rails at the time). Initially it seemed like many things were duplicated between backbone and rails, but as development progressed, I found this to be the exception rather than the rule. Especially if you're using Rails as an API, this kind of gem may only be helpful at the start of a project, and less relevant afterwards.

I say this because most of our models no longer have a 1-1 correspondence with our Rails resources. For example, while information on people is required, this information comes from a combination of a number of resources, and excludes much of the data in the primary people records. Also, creating and modifying them isn't appropriate in our app, and creation is by invitation only, and that's exposed as a different resource entirely.

Other resources aren't needed, and yet others have such a small subset of functionality or data, or have data from multiple resources, that having something generate them really wouldn't work.

Since rails is the API in our case, we don't have any duplication in the view layer.

I find that creating a custom generators that match the way you work with backbone, or having a few templates in your editor of choice to be a more flexible solution.

In terms of organization, we've added backbone as a directory under the app/assets/javascripts directory, and created directories for models, views, routers, helpers, etc. here. This allows the use of coffeescript to write backbone without any gems needed. We use the jasmine and jasminerice gems to test our backbone code with coffeescript and haml for fixtures (these live in the spec/javascripts directory}.

As for the templates, they are all rails view partials in our controllers that get rendered into the header. Backbone looks them up by id and uses them from there.

Hope this helps.

假装不在乎 2025-01-14 15:48:05

我刚刚得到消息,商业社区平台兴网正在打造这样一个宝石。

查看 github 上的存储库:https://github.com/xing/slouch

它没有记录然而,但我认为与构建它的人取得联系应该很容易。

i just got the information that the business community platform xing.com is building such a gem.

have a look at the repository on github: https://github.com/xing/slouch

it's not documented yet, but i think that it should be easy to get in touch with the guys building it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文