Skip to content

Commit e3963e7

Browse files
committed
Nearly done draft
1 parent 3b71989 commit e3963e7

File tree

7 files changed

+75
-29
lines changed

7 files changed

+75
-29
lines changed

Diff for: _posts/2011-10-10-organizing-backbone-using-modules.textile

+58-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ p. Unfortunatly Backbone.js does not tell you how to organize your code leaving
1111

1212
This was quite a different decision to other Javascript MVC frameworks who were more in favor of setting a development philosophy.
1313

14+
Hopefully this tutorial will allow you to build a much more robust project with great separation of concerns between design and code.
15+
1416
This tutorial will get you started on combining Backbone.js with "AMD":http://www.com (Asynchronous Module Definitions).
1517

1618
h3. What is AMD?
@@ -24,35 +26,73 @@ This tutorial will use "Require.js":http://requirejs.org to implement a modular
2426
Quick Overview
2527
* Modular
2628
* Scalable
29+
* Compiles well(see "r.js":http)
30+
* Market Adoption( Dojo 1.6 converted fully to AMD )
2731

2832
h3. Why Require.js?
2933

30-
p. Require.js has a great community.
34+
p. Require.js has a boastful community and is growing rapidly. James Burke the author updates Require.js almost daily and responds to user feedback always. He is also a contributer to the AMD specification.
35+
3136

3237
h3. Getting started
3338

3439
To easily understand this tutorial you should jump straight into the example code base.
3540

36-
h3. "Example Codebase":http://www.com
41+
h3. "Example Codebase":http://backbonetutorials.com/examples/modular-backbone
42+
43+
p. The tutorial is only loosely coupled with the example and you will find the example to be more comprehensive.
44+
45+
If you would like to see how a particuliar use case would be implemented please visit the Github page and create an issue.(Example Request: How to do nested views)
46+
47+
The example isn't super fleshed out but should give you a vague idea.
48+
49+
h3. Example File Structure
50+
51+
p. There are many different ways to lay out your files and I believe it is actually dependent on the size of the project. In the example below views and template s are mirroed in file structure. Collections and Models aren't categorized into folders.
3752

3853
{% highlight javascript %}
3954
/* File Structure
40-
├── assets
41-
│ ├── css
42-
│ │ └── main.css
43-
│ ├── images
44-
│ │ └── header.gif
45-
│ └── js
46-
│ └── libs
47-
│ ├── backbone.js
48-
│ ├── jquery.js
49-
│ └── underscore.js
50-
├── index.html
51-
└── src
52-
├── application.js
53-
└── modules
54-
├── friend.js
55-
└── message.js
55+
├── imgs
56+
├── css
57+
│ └── style.css
58+
├── templates
59+
│ ├── projects
60+
│ │ ├── list.html
61+
│ │ └── edit.html
62+
│ └── users
63+
│ ├── list.html
64+
│ └── edit.html
65+
├── js
66+
│ ├── libs
67+
│ │ ├── jquery
68+
│ │ │ ├── jquery.min.js
69+
│ │ │ └── jquery.js // jQuery Library Wrapper
70+
│ │ ├── backbone
71+
│ │ │ ├── backbone.min.js
72+
│ │ │ └── backbone.js // Backbone Library Wrapper
73+
│ │ └── underscore
74+
│ │ │ ├── underscore.min.js
75+
│ │ │ └── underscore.js // Underscore Library Wrapper
76+
│ ├── models
77+
│ │ ├── users.js
78+
│ │ └── projects.js
79+
│ ├── collections
80+
│ │ ├── users.js
81+
│ │ └── projects.js
82+
│ ├── views
83+
│ │ ├── projects
84+
│ │ │ ├── list.js
85+
│ │ │ └── edit.js
86+
│ │ └── users
87+
│ │ ├── list.js
88+
│ │ └── edit.js
89+
│ ├── router.js
90+
│ ├── app.js
91+
│ ├── main.js // Bootstrap
92+
│ ├── order.js //Require.js plugin
93+
│ └── text.js //Require.js plugin
94+
└── index.html
95+
5696
*/
5797
{% endhighlight %}
5898
p. To continue you must really understand what we are aiming towards as described in the introduction.
12 KB
Binary file not shown.

Diff for: examples/modular-backbone/js/collections/projects.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ define([
55
'models/projects'
66
], function($, _, Backbone, projectsModel){
77
var projectsCollection = Backbone.Collection.extend({
8-
Model: projectsModel,
8+
model: projectsModel,
99
initialize: function(){
1010

1111
}

Diff for: examples/modular-backbone/js/models/projects.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ define([
44
], function(_, Backbone) {
55
var projectsModel = Backbone.Model.extend({
66
defaults: {
7-
test: "tomasomas"
7+
score: 10
88
},
99
initialize: function(){
10-
console.log("Hi Nicola");
1110
}
1211

1312
});
14-
return new projectsModel;
13+
return projectsModel;
1514

1615
});

Diff for: examples/modular-backbone/js/router.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ define([
2525
showUsers: function(){
2626
userListView.render();
2727
},
28-
default: function(actions){
28+
defaultAction: function(actions){
2929
// We have no matching route, lets just log what the URL was
3030
console.log('No route:', actions);
3131
}

Diff for: examples/modular-backbone/js/views/projects/list.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ define([
1212
el: $("#page"),
1313
initialize: function(){
1414
this.collection = projectsCollection;
15-
this.collection.bind("add", this.something);
16-
this.collection = projectsCollection.add({ user: "Asd"});
15+
this.collection.bind("add", this.exampleBind);
16+
this.collection = projectsCollection.add({ name: "Twitter"});
17+
this.collection = projectsCollection.add({ name: "Facebook"});
18+
this.collection = projectsCollection.add({ name: "Myspace", score: 20});
1719
},
18-
something: function(){
19-
console.log("qwe");
20+
exampleBind: function( model ){
21+
console.log(model);
2022
},
2123
render: function(){
22-
var data = {};
24+
var data = {
25+
projects: this.collection.models,
26+
_: _
27+
};
2328
var compiledTemplate = _.template( projectListTemplate, data );
2429
$("#page").html( compiledTemplate );
2530
}
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<ul>
2-
<li>Project 1</li>
2+
<% _.each(projects, function(project){ %>
3+
<li><%= project.get("name") %> - <%= project.get("score") %></li>
4+
<% }); %>
35
</ul>

0 commit comments

Comments
 (0)