10

我想试试 Angularjs。但是,我一直难以决定应该将我的 Angular 应用程序放在哪里。

我在后端使用 Rails 框架。我已经看到了整个 Angular 应用程序位于 assets/javascript 文件夹下的教程。

我想知道是否可以将其完全放在我的 rails 目录之外,而不是放在 assets/javascript 文件夹中。这样一来,我就有可能将后端和前端完全分开。(这是推荐的吗?)。

我相信资产管道也预编译了很多资产。如果我要分离出 angularjs 资产,我是否需要以某种方式预编译资产?

谢谢

4

4 回答 4

6

我一直在处理一组类似的问题。有一些很好的工具可以让你将 AngularJS 直接集成到你的 rails 资产管道中,如果你只想要一点 Angular,它们对我来说看起来不错。

但是,如果您想要一个完整的 Angular 前端,也就是单页 Web 应用程序,我认为您最终会受到兼容性和一些工具的限制。我觉得 Rails gem 跟不上 Angular,所以你会遇到版本冲突。我还看到越来越多的 Angular 独立工具,我非常喜欢ng-boilerplate项目模板。我也喜欢很多测试工具,例如 karma,但我还没有真正找到将 karma 与 rails 集成的方法。

For that reason, I eventually decided that I'd keep the two separate. Initially, I did that through creating a rails application and a separate angular application (separate directories). I used ng-boilerplate as the framework for the angular end. I wrote a tutorial on that. This eventually got a bit frustrating, and I wrote some more thoughts on it, the main annoyance was that I had two git repositories and it was annoying to keep them in synch. It's also sort of annoying working with an IDE across two directories. I've ended up shifting to rails and angular being in the same folder, and they seem to play nice, as each uses different directories within that project.

In this current structure, I'm using the grunt setup that came with ng-boilerplate to minify all the code, package it and also run karma unit testing. I haven't yet nailed the end-to-end testing, but it's on my list. I've found this to be a relatively productive work environment. My chosen structure for my pages, controllers and karma test cases has some repeated code (I'm choosing not to factor it out to maintain readability). I'm planning to extend the rails scaffold generator to create the javascript framework for me - so when I create a persons rails scaffold, it will also create a persons angularjs scaffold for me. I'll update here if and when I do that work.

EDIT: I've completed the scaffolding work as well, which allows rails to automatically generate the angularJS elements when you generate the rails models/controllers etc. The blog post is here: http://technpol.wordpress.com/2013/09/24/rails-generator-to-generate-angular-views/

于 2013-09-24T01:37:49.010 回答
5

您可以使用基于 grunt 的工作流程:

如果你从一个解耦的前端开始,首先使用模拟,这样你就可以保持在角度范围内,并且不会失去在后端和前端逻辑之间切换的焦点。构建单页应用程序的一个优点是您可以独立于后端 api 来开发它。有关模拟 http 响应的信息,请参阅 ( http://docs.angularjs.org/api/ngMockE2E.$httpBackend )。

于 2013-08-10T17:15:06.400 回答
1

We have been using AngularJS with our Rails application, in a way where we have been using Rails ERB templates, but switch over to using ng directive as and when required.

For this above setup we have used bower/bower-rails gem, which lets us use bower to manage the angular packages and their dependencies. We commit this into our repo, in the javascripts directory, and is taken care of by the Rails asset pipeline.

This setup has worked well for us considering we have above 50-50 % split of our views between the ERB templates and Angularjs.

More about this setup in the links below:

于 2015-01-28T10:53:59.770 回答
0

There are many advantages of separating out your api service (rails in this case) and your frontend components. As we do for ios/android apps, angular client can live on its own as a separate entity. It will be a static website that can be deployed on s3 or any static website host. It just needs to communicate with your api service. You could setup CORS to make it possible.

Some advantages of this workflow

  • You could use rails-api, which is a subset of rails application. If you are just going to use rails to build apis, it doesnt make sense to have all functionality that a complete rails app provides. Its lightweight, faster and inclined more towards building API first arch than a MVC arch.
  • You could use yeoman angular-generator to generate an angular app and make the most of grunt & bower to manage build (concat,uglify,cdnify etc) and dependencies (angular modules).
  • Deployments will become flexible. You won't need to depend on one to push the other.
  • If you ever plan to change your backend stack (eg rails to play/revel), you would not need to worry about your client components.
  • By splitting the development of the frontend and the Rails backend you could distribute the work over two development teams and keep the application as a whole very extensible.

There is also one downside to this approach. By having the applications in two separate repositories, you can’t easily have a full integration test. So you will have to test the apps separately. You could mock your apis to test angular app.

We have been using this approach and would recommend others the same. Less dependency & more productivity.

于 2015-09-22T05:32:17.450 回答