开发者

Rails3 views- JS templating directory structure

I really don't like having a duplicate directory structure in the public folder to contain Javascript templates as is suggested here. I'm about to dive into a project. Anyone that can talk me down from putting all my JS views in with my other views, please chime in with reasons not to do this. My thoughts:

Whether you're using Backbone, Jammit templates, or any other Javascript to create a view of your data, shouldn't that code ideally live in开发者_开发技巧 the /app/views/[object] directory? If we're designing an application with multiple ways to represent the data, shouldn't all of those views live in the same place?

It doesn't make sense to set up routes and have rails serve the files of course, but if we're using Jammit/Closure/another JS compression tool then we've already added a layer of processing between our directory structure and the JS we transmit to the client. Shouldn't that mean we can put the templates where it makes the most sense for code organization/maintenance?

Thanks.


The reason it's suggested to not put the .js files in app/views/[object] is that they aren't part of the Rails application. They are actually part of the Backbone or Jammit application/framework, so they don't belong in the app directory. If the files were .js.erb files then they should be in the app directory, but since they aren't they belong in the public/javascripts directory.

There isn't anyone stopping you from doing this, but because they aren't .erb template files they don't really belong in the app directory. They are public .js files. The reason they aren't part of the core Rails app is that they don't end with .erb and they function on their own, regardless of controllers or ruby classes, like images and css files.

Keep in mind that if they are in the /app/views directory then they will have to be served through Rails. In other words, you'll have to have a controller server them and this adds some additional complexity and strain to your servers. What's more is that if they don't require ruby data to be passed from your controllers, to the views, you're going to have some pretty basic/pointless controllers that simply serve static data. And you'll probably have to rename the files to .js.erb so that they can be served via these controllers. If the files are served directly from the public directory this isn't necessary, so it's much simpler.

There is nothing wrong with one directory mirroring another, this happens a lot, for example with RSpec spec/models, spec/views, spec/controllers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜