var yourblog = {
account : {},
posts : [],
comments : []
}
// yourblog.js
var yourblog = {}
// account.js
yourblog.account = { stuff : {} }
// posts.js
yourblog.posts = { moreStuff : {} }
// comments.js
yourblog.comments = { blech : {} }
<script src="Module1.js"></script> <script src="Module2.js"></script> <script src="Module3.js"></script> <script src="app.js"></script>
<script src="Module1.js"></script> <script src="Module2.js"></script> <script src="Module3.js"></script> <script src="Module4.js"></script> <script src="Module5.js"></script> <script src="Module6.js"></script> <script src="Module7.js"></script> <script src="Module8.js"></script> <script src="Module9.js"></script> <script src="Module10.js"></script> <script src="Module11.js"></script> <script src="Module12.js"></script> <script src="app.js"></script>
<script src="Module1.js"></script> <script src="Module2.js"></script> <script src="Module3.js"></script> <script src="Module4.js"></script> <script src="Module5.js"></script> <script src="Module12.js"></script> <!-- want to use 6 in 12? --> <script src="Module6.js"></script> <script src="Module7.js"></script> <script src="Module8.js"></script> <script src="Module9.js"></script> <script src="Module10.js"></script> <script src="Module11.js"></script> <script src="app.js"></script>
<script src="Module1.js"></script> <script src="Module2.js"></script> <script src="Module3.js"></script> <script src="Module4.js"></script> <script src="Module9.js"></script> <!-- Did 9 depend on 12? --> <script src="Module5.js"></script> <script src="Module12.js"></script> <!-- 12 --> <script src="Module6.js"></script> <script src="Module7.js"></script> <script src="Module8.js"></script> <script src="Module10.js"></script> <script src="Module11.js"></script> <script src="app.js"></script>
// Ha, just kidding. // Right?
define(id?, dependencies?, factory);
define(['yourModule'], function(yourModule){
// You are in a module called 'myModule'
// That depends on and is provided 'yourModule'
});
require(['dependency'],function(dependency) {
// your module
);
curl(['js!nonAMD.js'])
.next(['dep1', 'dep2'], function (dep1, dep2) {
// execute this phase
})
.next(['dep3'])
.then(function () {
// Execute when everything is ready
});
<html>
<head>
<script data-main="scripts/main.js"
src="scripts/vendor/require.js"></script>
</head>
<body>
</body>
</html>
require(['dep1','dep2'],function(dep1,dep2){
// And away we go!
})
Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
documentcloudWithout your data, your app is nothing
Your data = bits of stuff
Backbone.Model.extend(properties [, classProperties]);
var Person = Backbone.Model.extend({
fullName : function() {
return this.get('fName') + ' ' + this.get('lName');
}
});
var cartoonist = new Person();
cartoonist.set('fName', 'Gary');
cartoonist.set('lName', 'Larson');
console.log(cartoonist.fullName());
model.on(event, callback [, context])
model.off([event] [, callback] [, context])
model.trigger(event [, *args])
model.save() model.fetch() model.destroy() // all delegate to model.sync();
var User = Backbone.Model.extend({});
var UserList = Backbone.Collection.extend({
model : User
});
Very same extend()
var users = new UserList();
users.create({ name : 'Jack' });
Just simple conventions around simple ideas.
Organizing your data so you don't have to.
Mostly convention and convenience, allowing you to move more quickly
Backbone.View.extend(properties [, classProperties]);
Still the same extend()
The heart of any view is its render() method which, by default, does nothing
Yes, nothing.
this.el; // The dom element you have this.$el; // a cached jQuery instance of your el this.$(); // a jQuery method scoped to your el
var MyView = Backbone.View.extend({
events : {
'click .btn' : 'onButtonClick'
},
onButtonClick : function(evt) {
// automatically called
}
});
var MyView = Backbone.View.extend({
initialize : function() {
this.collection.on('all', this.render, this);
},
render : function() {
// Do some rendering magic
}
});
Voila, a cheap re-render when your collection changes.
But you need something.