Skip to content

Commit

Permalink
vendor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
purge committed Apr 27, 2012
1 parent 6ee5156 commit b6ab336
Show file tree
Hide file tree
Showing 31 changed files with 29,100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .npmignore
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
30 changes: 30 additions & 0 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Set the require.js configuration for your application.
require.config({
// Initialize the application with the main application file
deps: ["main"],

paths: {
// JavaScript folders
libs: "../assets/js/libs",
plugins: "../assets/js/plugins",

// Libraries
jquery: "../assets/js/libs/jquery",
underscore: "../assets/js/libs/underscore",
backbone: "../assets/js/libs/backbone",

// Shim Plugin
use: "../assets/js/plugins/use"
},

use: {
backbone: {
deps: ["use!underscore", "jquery"],
attach: "Backbone"
},

underscore: {
attach: "_"
}
}
});
81 changes: 81 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require([
"namespace",

// Libs
"jquery",
"use!backbone",

// Modules
"modules/example"
],

function(namespace, $, Backbone, Example) {

// 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;
}
});
}
});

// Shorthand the application namespace
var app = namespace.app;

// 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.
$(function() {
// 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 &&
href.indexOf("javascript:") !== 0) {
// Stop the default event to ensure the link will not cause a page
// refresh.
evt.preventDefault();

// `Backbone.history.navigate` is sufficient for all Routers and will
// trigger the correct events. The Router's internal `navigate` method
// calls this anyways.
Backbone.history.navigate(href, true);
}
});

});
44 changes: 44 additions & 0 deletions app/modules/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
define([
"namespace",

// Libs
"use!backbone"

// Modules

// Plugins
],

function(namespace, Backbone) {

// Create a new module
var Example = namespace.module();

// Example extendings
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();

// If a done function is passed, call it with the element
if (_.isFunction(done)) {
done(view.el);
}
});
}
});

// Required, return the module for AMD compliance
return Example;

});
65 changes: 65 additions & 0 deletions app/namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
define([
// Libs
"jquery",
"use!underscore",
"use!backbone"
],

function($, _, Backbone) {
// Put application wide code here

return {
// 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) {
var JST = window.JST = window.JST || {};
var def = new $.Deferred();

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

return def.resolve(JST[path]);
}

// Fetch it asynchronously if not available from JST, ensure that
// template requests are never cached and prevent global ajax event
// handlers from firing.
$.ajax({
url: path,
type: "get",
dataType: "text",
cache: false,
global: false,

success: function(contents) {
JST[path] = _.template(contents);

// Set the global JST cache and return the template
if (_.isFunction(done)) {
done(JST[path]);
}

// Resolve the template deferred
def.resolve(JST[path]);
}
});

// Ensure a normalized return value (Promise)
return def.promise();
},

// Create a custom object with a nested Views object
module: function(additionalProps) {
return _.extend({ Views: {} }, additionalProps);
},

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

0 comments on commit b6ab336

Please sign in to comment.