Permalink
Browse files

initial work on init refactor

  • Loading branch information...
0 parents commit ce3a5312b86abf6856cb0c89c6d6eb135887778c @tbranyen tbranyen committed May 15, 2012
@@ -0,0 +1,3 @@
+[submodule "bbb/root"]
+ path = bbb/root
+ url = git://github.com/tbranyen/backbone-boilerplate.git
30 bbb.js
@@ -0,0 +1,30 @@
+exports.description = "Backbone Boilerplate framework";
+exports.notes = "This tool will help you install, configure, build, and "
+ + "maintain your Backbone Boilerplate project.";
+
+exports.template = function(grunt, init, done) {
+
+ // Grunt utilities.
+ var task = grunt.task;
+ var file = grunt.file;
+ var utils = grunt.utils;
+ var log = grunt.log;
+ var verbose = grunt.verbose;
+ var fail = grunt.fail;
+ var option = grunt.option;
+ var config = grunt.config;
+ var template = grunt.template;
+ var _ = grunt.utils._;
+
+ // Files to copy (and process).
+ var files = init.filesToCopy({});
+
+ // Actually copy (and process). files.
+ init.copyAndProcess(files, {}, {
+ noProcess: [ "assets/**", "test/**", "favicon.ico" ]
+ });
+
+ // All done!
+ done();
+
+};
@@ -0,0 +1,3 @@
+{
+ ".git": false
+}
Submodule root added at 240fee
@@ -0,0 +1,40 @@
+// 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: "_"
+ },
+
+ // Add the Backbone LocalStorage plugin in
+ "plugins/backbone-localstorage": {
+ deps: ["use!backbone"]
+ },
+
+ // Add the Backbone LocalStorage plugin in
+ "plugins/backbone.layoutmanager": {
+ deps: ["use!backbone"]
+ }
+ }
+});
@@ -0,0 +1,101 @@
+require([
+ "namespace",
+
+ // Libs
+ "jquery",
+ "use!backbone",
+
+ // Modules
+ "modules/todo"
+],
+
+function(namespace, $, Backbone, Todo) {
+
+ // An example Backbone application contributed by
+ // [Jérôme Gravel-Niquet](http://jgn.me/). This demo uses a simple
+ // [LocalStorage adapter](backbone-localstorage.js)
+ // to persist Backbone models within your browser.
+
+ // Defining the application router, you can attach sub routers here.
+ var Router = Backbone.Router.extend({
+ routes: {
+ "": "index"
+ },
+
+ index: function() {
+ // Create a new Todo List.
+ var list = new Todo.List();
+
+ // Create a new layout.
+ var main = new Backbone.LayoutManager({
+ template: "main",
+
+ views: {
+ // Attach the root content View to the layout.
+ "form": new Todo.Views.Form({
+ collection: list
+ }),
+
+ // Attach the stats View into the content View.
+ ".stats": new Todo.Views.Stats({
+ collection: list
+ }),
+
+ // Attach the list View into the content View.
+ ".list": new Todo.Views.List({
+ collection: list
+ })
+ }
+ });
+
+ window.main = main;
+
+ // Attach to the DOM
+ main.$el.appendTo("#main");
+
+ // Render
+ main.render();
+
+ // Fetch the data from localStorage
+ list.fetch();
+ }
+ });
+
+ // 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);
+ }
+ });
+
+});
@@ -0,0 +1,99 @@
+define([
+ "namespace",
+
+ // Libs
+ "use!backbone",
+
+ // Views
+ "modules/todo/views",
+
+ // Plugins
+ "use!plugins/backbone-localstorage"
+],
+
+function(namespace, Backbone, Views) {
+
+ // Create a new module
+ var Todo = namespace.module();
+
+ // Todo Model
+ // ----------
+
+ // Our basic **Todo** model has `content` and `done` attributes.
+ Todo.Model = Backbone.Model.extend({
+ // Default attributes for the todo.
+ defaults: {
+ done: false
+ },
+
+ // Toggle the `done` state of this todo item.
+ toggle: function() {
+ this.save({
+ done: !this.get("done")
+ });
+ },
+
+ // Remove this Todo from *localStorage* and delete its view.
+ clear: function() {
+ this.destroy();
+ },
+
+ // Ensure some content has been filled in
+ validate: function(attrs) {
+ if (!attrs.content) {
+ return "Missing content for todo item.";
+ }
+ }
+ });
+
+ // Todo Collection
+ // ---------------
+
+ // The collection of todos is backed by *localStorage* instead of a remote
+ // server.
+ Todo.List = Backbone.Collection.extend({
+ // Reference to this collection's model.
+ model: Todo.Model,
+
+ // Save all of the todo items under the `"todos"` namespace.
+ localStorage: new Store("todos-backbone"),
+
+ // Filter down the list of all todo items that are finished.
+ done: function() {
+ return this.filter(function(todo) {
+ return todo.get("done");
+ });
+ },
+
+ // Filter down the list to only todo items that are still not finished.
+ remaining: function() {
+ return this.without.apply(this, this.done());
+ },
+
+ // We keep the Todos in sequential order, despite being saved by unordered
+ // GUID in the database. This generates the next order number for new items.
+ nextOrder: function() {
+ if (!this.length) {
+ return 1;
+ }
+
+ return this.last().get("order") + 1;
+ },
+
+ // Todos are sorted by their original insertion order.
+ comparator: function(todo) {
+ return todo.get("order");
+ }
+ });
+
+
+ // Todo Views
+ // ----------
+
+ // Attach the Views sub-module into this module.
+ Todo.Views = Views;
+
+ // Required, return the module for AMD compliance
+ return Todo;
+
+});
Oops, something went wrong.

0 comments on commit ce3a531

Please sign in to comment.