Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 6862da2d3792943e9f784def0e099b10070e9efb @narze committed Jul 20, 2012
@@ -0,0 +1,83 @@
+define([
+ // Libraries.
+ "jquery",
+ "lodash",
+ "backbone",
+
+ // Plugins.
+ "plugins/backbone.layoutmanager"
+],
+
+function($, _, Backbone) {
+
+ // Provide a global location to place configuration settings and module
+ // creation.
+ var app = {
+ // The root path to run the application.
+ root: "/"
+ };
+
+ // Localize or create a new JavaScript Template object.
+ var JST = window.JST = window.JST || {};
+
+ // Configure LayoutManager with Backbone Boilerplate defaults.
+ Backbone.LayoutManager.configure({
+ paths: {
+ layout: "app/templates/layouts/",
+ template: "app/templates/"
+ },
+
+ fetch: function(path) {
+ path = path + ".html";
+
+ if (!JST[path]) {
+ $.ajax({ url: app.root + path, async: false }).then(function(contents) {
+ JST[path] = _.template(contents);
+ });
+ }
+
+ return JST[path];
+ }
+ });
+
+ // Mix Backbone.Events, modules, and layout management into the app object.
+ return _.extend(app, {
+ // Create a custom object with a nested Views object.
+ module: function(additionalProps) {
+ return _.extend({ Views: {} }, additionalProps);
+ },
+
+ // Helper for using layouts.
+ useLayout: function(name) {
+ // If already using this Layout, then don't re-inject into the DOM.
+ if (this.layout && this.layout.options.template === name) {
+ return this.layout;
+ }
+
+ // If a layout already exists, remove it from the DOM.
+ if (this.layout) {
+ this.layout.remove();
+ }
+
+ // Create a new Layout.
+ var layout = new Backbone.Layout({
+ template: name,
+ className: "layout " + name,
+ id: "layout"
+ });
+
+ // Insert into the DOM.
+ $("#main").empty().append(layout.el);
+
+ // Render the layout.
+ layout.render();
+
+ // Cache the refererence.
+ this.layout = layout;
+
+ // Return the reference, for chainability.
+ return layout;
+ }
+ }, Backbone.Events);
+
+});
@@ -0,0 +1,34 @@
+// 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",
+ lodash: "../assets/js/libs/lodash",
+ backbone: "../assets/js/libs/backbone"
+ },
+
+ shim: {
+ backbone: {
+ deps: ["lodash", "jquery"],
+ exports: "Backbone"
+ },
+
+ // Backbone.layoutmanager depends on Backbone.
+ "plugins/backbone.layoutmanager": ["backbone"],
+
+ // Backbone.localstorage depends on Backbone.
+ "plugins/backbone-localstorage": ["backbone"],
+
+ // Cross domain ajax
+ "plugins/jquery.xdomainajax": ["jquery"],
+
+ "plugins/toskoy": []
+ }
+});
@@ -0,0 +1,39 @@
+require([
+ // Application.
+ "app",
+
+ // Main Router.
+ "router"
+],
+
+function(app, Router) {
+
+ // 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, set the
+ // root folder to '/' by default. Change in app.js.
+ Backbone.history.start({ pushState: true, root: app.root });
+
+ // 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 absolute anchor href.
+ var href = $(this).attr("href");
+
+ // If the href exists and is a hash route, run it through Backbone.
+ if (href && href.indexOf("#") === 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. The fragment is sliced from the root.
+ Backbone.history.navigate(href, true);
+ }
+ });
+
+});
@@ -0,0 +1,104 @@
+define([
+ "app",
+
+ // Libs
+ "backbone",
+
+ // Views
+ "modules/todo/views",
+
+ // Plugins
+ "plugins/backbone-localstorage"
+],
+
+function(app, Backbone, Views) {
+
+ // Create a new module
+ var Todo = app.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,
+ me: true
+ },
+
+ // Toggle the `done` state of this todo item.
+ toggle: function() {
+ this.store({
+ 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");
+ },
+
+ clear: function() {
+ this.each(function(model) { model.destroy(); } );
+ return this.reset();
+ }
+ });
+
+ // 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 6862da2

Please sign in to comment.