|
25 | 25 | 1. [Constructors](#constructors)
|
26 | 26 | 1. [Modules](#modules)
|
27 | 27 | 1. [jQuery](#jquery)
|
| 28 | + 1. [Backbone](#backbone) |
28 | 29 | 1. [ES5 Compatability](#es5)
|
29 | 30 | 1. [Testing](#testing)
|
30 | 31 | 1. [Performance](#performance)
|
|
1261 | 1262 | **[[⬆]](#TOC)**
|
1262 | 1263 |
|
1263 | 1264 |
|
| 1265 | +## <a name='backbone'>Backbone</a> |
| 1266 | + |
| 1267 | + - Folder structure guidelines (from Rails 3 project root) |
| 1268 | + |
| 1269 | + ``` |
| 1270 | + app/ |
| 1271 | + └─┬ assets/ |
| 1272 | + └─┬ apps/ |
| 1273 | + ├── myCoolApp/ |
| 1274 | + ├── anotherCoolApp/ |
| 1275 | + ├── theCoolestApp/ |
| 1276 | + └─┬ app/ |
| 1277 | + ├─┬ views/ |
| 1278 | + │ ├── ListItemView.js |
| 1279 | + │ └── ListView.js |
| 1280 | + ├── models/ |
| 1281 | + ├── collections/ |
| 1282 | + ├── routers/ |
| 1283 | + └── index.js |
| 1284 | + config/ |
| 1285 | + db/ |
| 1286 | + lib/ |
| 1287 | + ... |
| 1288 | + ``` |
| 1289 | + |
| 1290 | + - The entry point for any Backbone app should be in the `index.js` file found in the root of the application's directory. |
| 1291 | + - Generally, it should follow the following structure: |
| 1292 | +
|
| 1293 | + ```javascript |
| 1294 | + // app/assets/apps/app/index.js |
| 1295 | + !function($, Backbone) { |
| 1296 | +
|
| 1297 | + var app = { |
| 1298 | + init: function() { |
| 1299 | + // application specific init code goes here |
| 1300 | + app.router = new app.Routers.ApplicationRouter(); |
| 1301 | + Backbone.history.start(); |
| 1302 | + }, |
| 1303 | + Views: {}, |
| 1304 | + Models: {}, |
| 1305 | + Collections: {}, |
| 1306 | + Routers: {}, |
| 1307 | + Helpers: {} |
| 1308 | + }; |
| 1309 | +
|
| 1310 | + $(function(){ |
| 1311 | + // document ready |
| 1312 | + app.init(); |
| 1313 | + })(); |
| 1314 | +
|
| 1315 | + }(jQuery, Backbone); |
| 1316 | + ``` |
| 1317 | +
|
| 1318 | + - Classes live in the `app` (or app name) object under the corresponding Backbone type (Models, Collections, Views, Routers) with PascalCase |
| 1319 | +
|
| 1320 | + ```javascript |
| 1321 | + // bad |
| 1322 | + ListItemView = Backbone.View.extend(); |
| 1323 | +
|
| 1324 | + // bad |
| 1325 | + app.ListItemView = Backbone.View.extend(); |
| 1326 | +
|
| 1327 | + // good |
| 1328 | + app.Views.ListItemView = Backbone.View.extend(); |
| 1329 | +
|
| 1330 | + // bad |
| 1331 | + app.Views.ApplicationRouter = Backbone.Router.extend(); |
| 1332 | +
|
| 1333 | + // good |
| 1334 | + app.Routers.ApplicationRouter = Backbone.Router.extend(); |
| 1335 | + ``` |
| 1336 | +
|
| 1337 | + - Instances live directly under the `app` object with camelCase |
| 1338 | +
|
| 1339 | + ```javascript |
| 1340 | + // bad |
| 1341 | + app.Views.messagesView = new app.Views.ListView(); |
| 1342 | +
|
| 1343 | + // good |
| 1344 | + app.messagesView = new app.Views.ListView(); |
| 1345 | + ``` |
| 1346 | +
|
| 1347 | + **[[⬆]](#TOC)** |
| 1348 | +
|
| 1349 | +
|
1264 | 1350 | ## <a name='es5'>ECMAScript 5 Compatability</a>
|
1265 | 1351 |
|
1266 | 1352 | - Refer to [Kangax](https://twitter.com/kangax/)'s ES5 [compatibility table](http://kangax.github.com/es5-compat-table/)
|
|
0 commit comments