Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Created a backbone boilerplate setup.
Added backbone-mongodb as it exists today.
  • Loading branch information
Viktor Nagy committed Feb 8, 2012
0 parents commit 1ab9c9c
Show file tree
Hide file tree
Showing 1,936 changed files with 280,462 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
npm-debug.log
.lock-wscript
build/node_modules/grunt/node_modules/glob/build
73 changes: 73 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(function() {

// Defining the application router, you can attach sub routers here.
var Router = Backbone.Router.extend({
routes: {
"": "index",
":hash": "index"
},

index: function(hash) {
var route = this;
var tutorial = new Example.Views.Tutorial();

// Attach the tutorial to the DOM
tutorial.render(function(el) {
$("#main").html(el);

// Fix for hashes in pushState and hash fragment
if (hash && !route._alreadyTriggered) {
// Reset to home, pushState support automatically converts hashes
Backbone.history.navigate("", false);

// Trigger the default browser behavior
location.hash = hash;

// Set an internal flag to stop recursive looping
route._alreadyTriggered = true;
}
});
}
});

// Treat the jQuery ready function as the entry point to the application.
// Inside this function, kick-off all initialization, everything up to this
// point should be definitions.
jQuery(function($) {
// Shorthand the application namespace
var app = namespace.app;

// Include the example module
var Example = namespace.module("example");

// Define your master router on the application namespace and trigger all
// navigation from this instance.
app.router = new Router();

// Trigger the initial route and enable HTML5 History API support
Backbone.history.start({ pushState: true });

// All navigation that is relative should be passed through the navigate
// method, to be processed by the router. If the link has a data-bypass
// attribute, bypass the delegation completely.
$(document).on("click", "a:not([data-bypass])", function(evt) {
// Get the anchor href and protcol
var href = $(this).attr("href");
var protocol = this.protocol + "//";

// Ensure the protocol is not part of URL, meaning its relative.
if (href && href.slice(0, protocol.length) !== protocol) {
// Stop the default event to ensure the link will not cause a page
// refresh.
evt.preventDefault();

// This uses the default router defined above, and not any routers
// that may be placed in modules. To have this work globally (at the
// cost of losing all route events) you can change the following line
// to: Backbone.history.navigate(href, true);
app.router.navigate(href, true);
}
});
});

})();
38 changes: 38 additions & 0 deletions app/modules/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Use an IIFE...
// (http://benalman.com/news/2010/11/immediately-invoked-function-expression/)
// to assign your module reference to a local variable, in this case Example.
//
// Change line 16 'Example' to the name of your module, and change line 38 to
// the lowercase version of your module name. Then change the namespace
// for all the Models/Collections/Views/Routers to use your module name.
//
// For example: Renaming this to use the module name: Project
//
// Line 16: (function(Project) {
// Line 38: })(namespace.module("project"));
//
// Line 18: Project.Model = Backbone.Model.extend({
//
(function(Example) {

Example.Model = Backbone.Model.extend({ /* ... */ });
Example.Collection = Backbone.Collection.extend({ /* ... */ });
Example.Router = Backbone.Router.extend({ /* ... */ });

// This will fetch the tutorial template and render it.
Example.Views.Tutorial = Backbone.View.extend({
template: "app/templates/example.html",

render: function(done) {
var view = this;

// Fetch the template, render it to the View element and call done.
namespace.fetchTemplate(this.template, function(tmpl) {
view.el.innerHTML = tmpl();

done(view.el);
});
}
});

})(namespace.module("example"));
47 changes: 47 additions & 0 deletions app/namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Change *namespace* to your namespace!
// This contains the module definition factory function, application state,
// events, and the router.
this.namespace = {
// Assist with code organization, by breaking up logical components of code
// into modules.
module: function() {
// Internal module cache.
var modules = {};

// Create a new module reference scaffold or load an existing module.
return function(name) {
// If this module has already been created, return it.
if (modules[name]) {
return modules[name];
}

// Create a module and save it under this name
return modules[name] = { Views: {} };
};
}(),

// This is useful when developing if you don't want to use a
// build process every time you change a template.
//
// Delete if you are using a different template loading method.
fetchTemplate: function(path, done) {
window.JST = window.JST || {};

// Should be an instant synchronous way of getting the template, if it
// exists in the JST object.
if (JST[path]) {
return done(JST[path]);
}

// Fetch it asynchronously if not available from JST
return $.get(path, function(contents) {
var tmpl = _.template(contents);
JST[path] = tmpl;

done(tmpl);
});
},

// Keep active application instances namespaced under an app object.
app: _.extend({}, Backbone.Events)
};
Loading

0 comments on commit 1ab9c9c

Please sign in to comment.