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.
Web programming is the science of coming up with increasingly complicated ways of concatenating strings.
— Greg Brockman (@thegdb) December 18, 2011
Demo application
function Module () {
return {
someStuff: { ... },
foo: function() { ... }
};
}
Your need for modules is directly related to how much of your app you can hold in your head at once.— Alex Sexton
|+ App
|- app.js
|- controllers.js
|- models.js
|- views.js
|- index.html
var UserModel = require('Models/User');
var UserView = require('Views/User');
Which results in this file structure
|+ App
|+ Models
|- User.js
|+ Views
|- User.js
var UserModel = require('UserModel');
var UserView = require('UserView');
Which results in this file structure
|+ App
|- UserModel.js
|- UserView.js
var UserModel = require('User/Model');
var UserView = require('User/View');
Which results in this file structure
|+ App
|+ User
|- Model.js
|- View.js
var UsersModel = require('modules/Users/Model');
var UsersView = require('modules/Users/views/MyView');
var Users = require('modules/Users');
|+ App
|- modules
|- Users
|- views
|- MyViews.js
|- Model.js
|- Collection.js
|- Router.js
|- Users.js
# Libs to require
define ['backbone'], (Backbone) ->
# Create a simple module
Venue = Views: {}
class Venue.Model extends Backbone.Model
defaults:
categories: []
name: 'Tasty Coffee Cafe'
Venue # Return the module for AMD compliance
C:\dev\myapp>bbb debug <--- "Executes debug build target"
Running "clean:0" (clean) task
Removing: dist/
Running "lint:files" (lint) task
Lint free.
Running "jst:dist/debug/templates.js" (jst) task
File "dist/debug/templates.js" created.
Running "requirejs" task
C:/dev/myapp/dist/debug/require.js
----------------
C:/dev/myapp/assets/js/libs/jquery.js
C:/dev/myapp/assets/js/libs/lodash.js <--- "Swap with underscore.js"
C:/dev/myapp/assets/js/libs/backbone.js
C:/dev/myapp/assets/js/plugins/backbone.layoutmanager.js
C:/dev/myapp/app/app.js
C:/dev/myapp/app/router.js
C:/dev/myapp/app/main.js
C:/dev/myapp/app/config.js
Running "concat:dist" (concat) task
File "dist/debug/require.js" created.
Done, without errors.
# Define the Item View for each Model
class ItemView extends Backbone.View
tagName: 'li'
template: _.template $('#item-template').html()
initialize: ->
@model.on 'change', @render, @
@model.on 'destroy', @remove, @
render: ->
@$el.html @template( @model.toJSON() )
@
clear: ->
@model.clear()
# Define the List View which creates Item View(s)
class ListView extends Backbone.View
tagName: 'ul'
template: _.template $('#list-template').html()
initialize: ->
@collection.on 'add', @addItem, @
@collection.on 'remove', @removeItem, @
@collection.on 'reset', @render, @
addItem: -> # Add item to local model-to-view children map
removeItem: -> # Remove item from above map
render: ->
@$el.html ''
@collection.each (model) ->
view = new ItemView model: model
@$el.append view.render().el
class MItemView extends Backbone.Marionette.ItemView
tagName: 'li'
template: '#item-template'
class MListView extends Backbone.Marionette.CollectionView
itemView: MItemView