Backbone.js 与 Rails
我目前正在尝试 Backbone.js 和 Rails 应用程序。我的问题是,我不知道如何使用我的 Rails 应用程序实现 Backbone 控制器和视图。我读过很多教程,但他们总是只使用backbone.js 中的一个控制器。
例如,我有两个导轨控制器。
- 活动控制器
- 包括两个视图:一个谷歌地图和一个搜索字段。谷歌地图插入了主干视图,搜索字段采用 HTML 格式,并通过主干视图获取其功能。
- 搜索字段应从我的 Rails 模型中获取数据并在地图内显示标记。
另一种是
- 用户控制器
- 此处查看用户个人资料,我想添加一些 ajax 功能,例如更新值等
在我的 application.js 中,我使用以下命令启动应用程序
var App = {
Views: {},
Controllers: {},
Collections: {},
init: function() {
new App.Controllers.Activities();
new App.Controllers.Users();
Backbone.history.start();
}
};
$(function() {
App.init();
});
问题是,我不需要在我的应用程序中使用活动控制器Rails 活动控制器中的用户配置文件和用户控制器。我该如何解决这个问题?我应该尝试读取 javascript 中的当前 URL,然后决定使用哪个控制器吗? 或者我应该将 JavaScript 文件放入 application.html.erb 中,然后在此处决定应使用哪个控制器?
或者这是使用backbone.js控制器的错误方法?
我对backbone.js 的结构有什么误解吗?或者我是否以错误的方式使用控制器?
另一个问题是,如何通过 Backbone.js 添加少量 JavaScript,特别是 jQuery 功能?例如,当用户单击字段时,我想删除字段内的标签。或者我想做一些选项卡功能,只是切换某些元素的可见性。 我应该为每个使用 javascript 的元素创建一个 Backbone 视图吗?或者这是超载?
希望我说清楚了,任何人都可以提供帮助, 谢谢!
I'm currently trying Backbone.js along with a rails app. My problem is, that I don't know how to implement the Backbone controllers and views with my rails app. I've read a lot of tutorials, but they are always using just one controller in backbone.js.
For example, I have two controllers in rails.
- Activities Controller
- Includes two views, a google map and a search field. The google map is inserted with a backbone view, the searchfield is in HTML and gets its functionality through a backbone view.
- The search field should fetch data from my rails model and display markers inside the map.
And the other one is
- Users Controller
- Here the users profile is viewed, and I want to add some ajax functionality like updating values and other things
In my application.js I start the app using
var App = {
Views: {},
Controllers: {},
Collections: {},
init: function() {
new App.Controllers.Activities();
new App.Controllers.Users();
Backbone.history.start();
}
};
$(function() {
App.init();
});
The problem is, that I don't need the Activities controller in my User Profile and the Users controller in the Rails Activities controller. How could I solve this? Should I try reading the current URL within javascript and then decide which controller is used?
Or should I put the JavaScript file into the application.html.erb and then decide here which controller should be used?
Or is this the wrong way to use backbone.js controllers?
Am I getting sth wrong with the structure of backbone.js? Or am I using the Controllers in a wrong way?
Another question is, how to add little JavaScript, in particular jQuery, functionality through Backbone.js? For example, I want to remove the label inside a field, when the user clicks into the field. Or I want to do some tab-functionality and just toggle the visibility of some elements.
Should I create for each element that is using javascript a Backbone view? Or is this overload?
Hope I made myself clear and anybody can help,
thx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不利用 Backbone 提供的路由功能来决定调用哪个方法呢?活动控制器将仅包含用于活动的路由,仅包含用于用户处理的用户控制器,等等。
像这样,您可以像您一样实例化控制器,路由将根据当前位置的哈希来决定发生什么。
如果您无法使用带有哈希值的链接(或者您的页面上没有此类链接),我只需将视图容器命名得足够具体,以便在需要时仅为当前视图附加事件。
IMO 中的 jQuery 插件等属于视图。您的选项卡和输入提示切换也是如此。
更新
一般而言(我不一定建议这样做):如果您有两种方法:
并且只想根据当前情况调用其中一种方法Rails 控制器,创建一个小助手:
例如在 *helpers/application_helper.rb* 中
,然后在布局文件(或标头部分)中调用此方法:
并使用例如 jQuery 来“拆分”您的 JS 执行:
Why not make use of the routes feature Backbone provides to decide which method to call? The activities controller would contain only routes use for activities, the user controller only for the user handling, and so forth.
Like this you can instantiate the controller just as you do and the routing will decide what happens based on the current location's hash.
If you can't use links with hashes (or there are no such links on your page), I'd simply name my view containers specific enough to attach events only for the current view when needed.
jQuery plugins etc. belong into views IMO. Same goes for your tabs and input hint toggle.
Update
On a general level (and I would not necessarily recommend doing it this way): If you have two methods:
and want to call only one of them depending on the current Rails controller, create a small helper:
e.g. in you *helpers/application_helper.rb*
then call this method in your layout file (or header partial):
and use e.g. jQuery to "split" your JS execution:
我个人使用 jammit(来自backbone的同一作者)。您可以按模块对样式表和 javascript 文件进行分组,并在不同的页面上使用它们。因此,您为活动视图创建一个模块,为用户视图创建另一个模块,每个模块都需要必要的 JavaScript 文件。关于此的两个好处:
这是在不创建单页 Web 应用程序时要采用的方法,在该应用程序中您依赖 # 个路由从一个控制器导航到另一个控制器。在您的情况下,您的主轨应用程序内有多个单页应用程序。
I personally use jammit (from the same author of backbone). You can group stylesheets and javascripts files by module and use them on your different pages. So you create a module for your activities view and another for your user view each requiring the necessary JavaScript file. Two nice things about this:
This is the way to go when not creating a single page web app where you relies on # routes to navigate from one controller to another. In your case you have multiple single page apps inside a main rails app.
我是 Backbone.js 新手,所以如果我错了,请有人纠正我,但我认为您对 Backbone 控制器的用途感到困惑。
Backbone.js 控制器基本上由 hashbang 路由(类似于 Rails 路由)和这些路由调用的操作组成。例如,如果您有一个 Rails 路线,它将在 http://mysite.com/backbone_test 处呈现视图,并且您有以下主干路由:
那么如果您转到 http://mysite.com/backbone_test# foo/THIS-IS-AMAZING 然后 MyController.MyFirstFunction 被执行,并且“THIS-IS-AMAZING”被记录在 JS 控制台中。
除非您直接需要这种与 hashbang 相关的功能,例如通过 url 保存 JavaScript 应用程序的状态(例如:http://my.app.com/#key=value&key=value&ad-infinitum=true )我建议不要使用骨干控制器。您可以通过模型集合和视图获得您描述的所有功能。
总体而言,控制器和骨干网的优点在于其模块化且不同的部分可以独立使用。
我想这个答案的总结是,如果您没有单页 javascript 应用程序,请不要使用 Backbone 控制器,而是依赖您的 Rails 控制器。
Im a Backbone.js newb, so please someone correct me if Im wrong, but I think youre confusing what Backbone controllers are used for.
Backbone.js controllers basically consists of hashbang routes(similar to Rails routes) and actions that these routes call. For instance if you have a rails route that will render a view at http://mysite.com/backbone_test and you have a following backbone route:
then if you go to http://mysite.com/backbone_test#foo/THIS-IS-AMAZING then MyController.MyFirstFunction gets executed and "THIS-IS-AMAZING" gets logged in JS console.
Unless you have a direct need for this sort of hashbang-related functionality, such as saving a state of your JavaScript application via an url (for example: http://my.app.com/#key=value&key=value&ad-infinitum=true ) Id advise against using Backbone controllers. You can get all of the functionality u described via Models Collections and Views.
Nice thing about Controllers and Backbone in general is that its modular and different parts can be used independently.
I guess the summary of this answer is that if you dont have a single page javascript application, do not use Backbone Controllers, rely on your rails controllers instead.