Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js directory structure

New to Javascript/Backbone. I am wondering what's the 'convention' in Backbone when setting up directory structure.

I have a Backbone.js app that has two main 'entry' points. One is Admin (admin.mydomain.com), and other is User (user.mydomain.com). Now I am confused about how to name the files/directories.

In particular, is it better to do this:

-views
--admin
----items.js
--user
----items.js

-templates
--admin
----items.html
--user
----user.html

--models
--collections

or

-admin
--views
----item.js
--templates
----item.html

-user
--views
----item.js
--templates
----item.html

--models
--collections

Also, if I have a directory with 2 routers, and I don't want to create 2 seperate directories just to house 1 file in each just to seperate them, how should I name them? For example, I have a directory routers which contains two files, a router for admin, and router for user. Should I have:

  • 2 seperate directories named user and admin within the directory router, and each directory contains router.js
  • just 2 files in router directory named admin-router.js and user-router.js.

Also, when is preffered to name a file admin.router.js or admin-router.js ?

Thanks!

like image 739
0xSina Avatar asked Dec 04 '13 23:12

0xSina


1 Answers

This will probably get closed as "not a real question" or something similar, because it's a stylistic thing that, depending on your point of view, either doesn't have a correct answer or has a different correct answer for every project.

I'm a big fan of the second option, because it means that different people can work on different parts of the project independently. If you happen to write a utility package, it can be dropped into a different project easily rather than having to put three different files all into different places. And, by the same token, it's Bower-friendly, should that be appropriate for your use case.

The advantage of the first approach is that not everything will fit nicely into sections like that. What happens when you have models that both user and admin rely on? You're stuck with either duplicating code, having one with a hard dependency on the other just for that one file, or separating it out into its own module, which quickly deteriorates into a completely flat structure if taken to its logical conclusion.

The Google-friendly terms you're looking for are "package by feature" and "package by layer" if you want to learn more, or if you just enjoy reading Internet slapfights.

like image 114
jfmatt Avatar answered Nov 15 '22 11:11

jfmatt