骨干与Rails(Backbone With Rails)

编程入门 行业动态 更新时间:2024-10-27 00:26:14
骨干与Rails(Backbone With Rails)

我从服务中获取数据,然后将数据设置为我的bean对象,并希望在我的视图中使用相同的数据,现在以rails视图的形式显示它。 现在我想使用骨干来验证这个表单。

我是Rails和Backbone的新手。

请告诉我如何继续这样做。

I'm getting my data from a service and then setting the data to my bean object and want to using the same data in my view displaying it in a form in rails view now. Now I want to validate this form using backbone.

I'm new to both Rails and Backbone.

Kindly give me an idea on how to proceed on this.

最满意答案

我想也许您对Web应用程序的工作方式感到困惑。 Backbone是一个客户端框架; 它使用在用户浏览器中运行的Javascript代码。 Rails是一个服务器端框架; 它使用在您的服务器上运行的Ruby代码。

鉴于此,根据定义,您的Backbone代码和Rails代码必须完全分开。 这两者只能通过两种方式进行通信:

1)你的Rails代码可以将<script>标签写入页面(在.html.erb文件中)并将变量数据放在那里; 例如:

<script> var myVarFromRails = '<%= someRailsVariable %>'; </script>

当它从服务器返回时(即,当您查看源页面时)将转换为:

<script> var myVarFromRails = 'foo'; </script>

(假设'foo'是someRailsVariable的值)。

2)您的Javacript代码可以向Rails URL发出AJAX请求,无论Rails代码吐出什么,都会返回作为对您的AJAX请求的响应。 换句话说,你可以这样做:

$.ajax({url: someRailsUrl, complete: function(response) { // whatever the server sent back will be inside the "response" variable }});

除此之外,两者几乎是完全分开的,如果你想在两者中做同样的事情(例如验证表单),你基本上必须编写两次代码,一次用于Ruby,一次用于Javascript。

我说“基本上”是因为有Rails插件以不同的方式为你做#1和#2。 我不是Rails专家,即使我有这么多插件,你真的需要自己寻找,找出你的代码库存在什么和有意义的东西。

希望有所帮助。

*编辑*

我知道我只是说我不会列出库,但后来我意识到如果我至少提供了一些可以帮助你入门的话会更有帮助。 只是不要把这些当作经典; 他们目前只是一些受欢迎的图书馆,但它们可能适合您,也可能不适合您。

https://github.com/codebrew/backbone-rails https://github.com/meleyal/backbone-on-rails https://github.com/aflatter/backbone-rails https://learn.thoughtbot.com/products/1-backbone-js-on-rails http://kiranb.scripts.mit.edu/backbone-slides/

最后两个实际上不是库,它们是一本书/演示文稿,但我认为它们可能有用。

I think perhaps you are confused about how web applications work. Backbone is a client-side framework; it uses Javascript code that is run in your users' browsers. Rails is a server-side framework; it uses Ruby code that runs on your server.

Given all that, your Backbone code and your Rails code by definition have to be completely separate. The two can communicate in only two ways:

1) Your Rails code can write <script> tags to the page (inside a .html.erb file) and put variable data there; for instance:

<script> var myVarFromRails = '<%= someRailsVariable %>'; </script>

When that comes back from the server (ie. when you view source the page) that will get converted to:

<script> var myVarFromRails = 'foo'; </script>

(assuming 'foo' was the value of someRailsVariable).

2) Your Javacript code can make AJAX requests to Rails URLs, and whatever the Rails code spits out there will come back as the response to your AJAX request. In other words you can do:

$.ajax({url: someRailsUrl, complete: function(response) { // whatever the server sent back will be inside the "response" variable }});

Other than that the two are pretty much entirely separate, and if you want to do the same thing in both of them (eg. validate a form) you essentially have to write the code twice, once for Ruby and once for Javascript.

I say "essentially" because there are Rails plug-ins which do #1 and #2 for you in different ways. I'm not a Rails expert, and even if I was there are so many of these plug-ins that you really need to look for yourself to find out what exists and what makes sense for your codebase.

Hope that helps.

* EDIT *

I know I just said I wouldn't list libraries, but then I realized it'd be more helpful if I at least provided a few to get you started. Just don't take these as canon; they're simply some popular libraries at the moment, but they may or may not be right for you.

https://github.com/codebrew/backbone-rails https://github.com/meleyal/backbone-on-rails https://github.com/aflatter/backbone-rails https://learn.thoughtbot.com/products/1-backbone-js-on-rails http://kiranb.scripts.mit.edu/backbone-slides/

That last two aren't actually libraries, they're a book/presentation, but I thought they might be useful.

更多推荐

本文发布于:2023-04-28 04:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329843.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:骨干   Rails   Backbone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!