对于单页应用程序,我真的需要 Rails 中的 MVC 框架(例如 Backbone.js 或 Ember.js)吗?

发布于 2025-01-04 17:18:46 字数 1432 浏览 1 评论 0原文

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

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

发布评论

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

评论(2

梦行七里 2025-01-11 17:18:46

那么你想实现什么目标?页面不再刷新?在这种情况下,rack-pjax 应该可以工作。如果您想要更快的速度或响应式用户界面,那么恐怕这不是一个好的解决方案。

在我看来,单页应用程序是首选,因为它严重降低了服务器上的负载和复杂性+ viewlogic 和 viewlogic 之间有一个很好的抽象。服务器逻辑。

您的服务器基本上是一个 API,您的客户端会将所有 API 数据呈现给浏览器。这样,服务器就得到了极大的简化,并且要做的工作也少了很多。 (赢了!)

在客户端我们也可以看到很多改进。如果做得正确,它会根据事件和对数据所做的状态更改不断地重新呈现自身。这种方法可以大大减少 UI 层中的耦合(和重复),并为用户提供更具响应性的 UI。 (胜利!)

如果您不太关心这一点,那么请继续使用 pjax :)

请记住,使用 JS 前端将现有的服务器端视图渲染应用程序重写为单页应用程序是一件棘手的事情。它可能最终会进行重大重写。您还可以尝试在 JS 前端中仅编写页面的一部分。

Well what do you want to achieve? No more page refreshes? In that case rack-pjax should work. If you want more speed or a responsive UI then I'm afraid it won't be a good solution.

A single page app is preferred imo because it seriously decreases the load and complexity on the server + there is a nice abstraction between viewlogic & serverlogic.

Your server would basically be an API and your client would render all of the API data to the browser. This way the server is heavily simplified and has a lot less work to do. (win!)

On the client side we can also see a lot of improvements. If done right it rerenders itself continuously based on events and statechanges made to the data. This approach leads to much less coupling (and duplication) in your UI layer and a more responsive UI for the users. (win!)

If you don't care much about that then go ahead and use pjax :)

Keep in mind though that rewriting an existing server-side-view-rendering app to a single page app using a JS frontend is tricky business. It'll probably end up in a major rewrite. You could also experiment with writing just parts of your page in a JS frontend.

小梨窩很甜 2025-01-11 17:18:46

虽然rack-pjax确实阻止浏览器在每次请求时刷新页面,但服务器仍然会在每次请求时发送整个HTML页面。如果您的目标是没有页面刷新的“单页”应用程序,rack-pjax 可以工作,但如果您使用处理 JSON 而不是加载的框架,您将节省大量带宽并拥有响应速度更快的应用程序整个页面。

对于一个简单的应用程序,我建议从 Backbone 或 Spine 等开始。对于更复杂的应用程序,您很快就会发现自己使用这些较小的框架编写了大量样板代码,并且您最好让 Ember 或 Cappuccino 之类的东西为您处理大部分繁重的工作。

如果您想要一个单页应用程序,那么您的部分或大部分逻辑将位于前端,要么直接用 JavaScript 编写,要么用 CoffeeScript 编写并编译为 JavaScript。当然,您仍然需要服务器上的某些逻辑(例如验证......即使在 JS 代码中使用验证逻辑,也始终假设人们可以并且将会向服务器发送错误数据)。

While rack-pjax does prevent the browser from refreshing the page with each request, the server is still sending an entire HTML page with each request. If your goal is a "single page" app with no page refreshes, rack-pjax will work but you're going to save a lot of bandwidth and have a much more responsive app if you use a framework that deals with JSON instead of loading an entire page.

For a simple app, I'd recommend starting with something like Backbone or Spine. For more complicated apps, you'll quickly find yourself writing a lot of boilerplate code with those smaller frameworks and you'd probably be better off letting something like Ember or Cappuccino handle most of the heavy lifting for you.

If you want a single-page app, it's a given that some or most of your logic is going to be on the frontend, either written directly in javascript or written in coffeescript and compiled to javascript. Of course, you do still need certain logic at the server (like validations.. always assume, even with validation logic in your JS code, that people can and will send bad data to the server).

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