Permalink
Browse files

committing backbone marionette reference app

  • Loading branch information...
1 parent 99ebd59 commit 4a049b5b21fbbb40e7830a08b10109c79e337e46 @jsoverson jsoverson committed Aug 24, 2012
Showing with 20,134 additions and 0 deletions.
  1. +50 −0 labs/dependency-examples/backbone_marionette_require/app.build.js
  2. +10 −0 labs/dependency-examples/backbone_marionette_require/css/custom.css
  3. +29 −0 labs/dependency-examples/backbone_marionette_require/index.html
  4. +50 −0 labs/dependency-examples/backbone_marionette_require/js/app.js
  5. +22 −0 labs/dependency-examples/backbone_marionette_require/js/collections/TodoList.js
  6. +11 −0 labs/dependency-examples/backbone_marionette_require/js/controllers/index.js
  7. +136 −0 labs/dependency-examples/backbone_marionette_require/js/lib/backbone-localStorage.js
  8. +1,431 −0 labs/dependency-examples/backbone_marionette_require/js/lib/backbone.js
  9. +1,346 −0 labs/dependency-examples/backbone_marionette_require/js/lib/backbone.marionette.js
  10. +35 −0 labs/dependency-examples/backbone_marionette_require/js/lib/require.js
  11. +205 −0 labs/dependency-examples/backbone_marionette_require/js/lib/tpl.js
  12. +1,059 −0 labs/dependency-examples/backbone_marionette_require/js/lib/underscore.js
  13. +34 −0 labs/dependency-examples/backbone_marionette_require/js/main.js
  14. +22 −0 labs/dependency-examples/backbone_marionette_require/js/models/Todo.js
  15. +10 −0 labs/dependency-examples/backbone_marionette_require/js/routers/index.js
  16. +11 −0 labs/dependency-examples/backbone_marionette_require/js/templates.js
  17. +13 −0 labs/dependency-examples/backbone_marionette_require/js/templates/footer.tmpl
  18. +2 −0 labs/dependency-examples/backbone_marionette_require/js/templates/header.tmpl
  19. +6 −0 labs/dependency-examples/backbone_marionette_require/js/templates/todoItemView.tmpl
  20. +3 −0 labs/dependency-examples/backbone_marionette_require/js/templates/todoListCompositeView.tmpl
  21. +4 −0 labs/dependency-examples/backbone_marionette_require/js/vent.js
  22. +16 −0 labs/dependency-examples/backbone_marionette_require/js/views/ActiveCount.js
  23. +22 −0 labs/dependency-examples/backbone_marionette_require/js/views/Footer.js
  24. +27 −0 labs/dependency-examples/backbone_marionette_require/js/views/Header.js
  25. +54 −0 labs/dependency-examples/backbone_marionette_require/js/views/TodoItemView.js
  26. +43 −0 labs/dependency-examples/backbone_marionette_require/js/views/TodoListCompositeView.js
  27. +15,483 −0 labs/dependency-examples/backbone_marionette_require/r.js
@@ -0,0 +1,50 @@
+
+
+({
+ appDir: "js/",
+ baseUrl: "./",
+ mainConfigFile: 'js/main.js',
+ out: "main.built.js",
+
+ //The directory path to save the output. If not specified, then
+ //the path will default to be a directory called "build" as a sibling
+ //to the build file. All relative paths are relative to the build file.
+ // dir: "../some/path",
+ locale: "en-us",
+ optimize: "uglify",
+
+ uglify: {
+ toplevel: true,
+ ascii_only: true,
+ beautify: true,
+ max_line_length: 1000
+ },
+
+ optimizeCss: "standard.keepLines",
+
+ inlineText: true,
+ useStrict: false,
+
+ // Breaks anything that defines a 'require'
+ //namespace: 'commit',
+ skipPragmas: false,
+ skipModuleInsertion: false,
+ stubModules: ['text'],
+ optimizeAllPluginResources: false,
+ findNestedDependencies: false,
+ removeCombined: true,
+
+ modules: [
+ {
+ name: "main"
+ }
+ ],
+
+
+ fileExclusionRegExp: /^\./,
+
+ preserveLicenseComments: true,
+
+ logLevel: 0,
+
+});
@@ -0,0 +1,10 @@
+#todo-list.filter-active .completed {
+ display:none
+}
+#todo-list.filter-completed .active {
+ display:none
+}
+
+#main, #footer {
+ display : none;
+}
@@ -0,0 +1,29 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
+ <title>Backbone.Marionette & Requirejs • TodoMVC</title>
+ <link rel="stylesheet" href="../../../assets/base.css">
+ <link rel="stylesheet" href="css/custom.css">
+ <!--[if IE]>
+ <script src="../../../assets/ie.js"></script>
+ <![endif]-->
+</head>
+<body>
+<section id="todoapp">
+ <header id="header">
+ </header>
+ <section id="main">
+ </section>
+ <footer id="footer">
+ </footer>
+</section>
+<footer id="info">
+ <p>Double-click to edit a todo</p>
+ <p>Created by <a href="http://github.com/jsoverson">Jarrod Overson</a></p>
+</footer>
+<script src="../../../assets/base.js"></script>
+<script data-main="js/main" src="js/lib/require.js"></script>
+</body>
+</html>
@@ -0,0 +1,50 @@
+define(
+ ['marionette','vent','collections/TodoList','views/Header','views/TodoListCompositeView','views/Footer'],
+ function(marionette, vent, TodoList, Header, TodoListCompositeView, Footer){
+ "use strict";
+
+ var app = new marionette.Application(),
+ todoList = new TodoList();
+
+ todoList.fetch();
+
+ app.addRegions({
+ header : '#header',
+ main : '#main',
+ footer : '#footer'
+ });
+
+ app.addInitializer(function(){
+
+ var viewOptions = {
+ collection : todoList
+ };
+
+ app.header.show(new Header(viewOptions));
+ app.main.show(new TodoListCompositeView(viewOptions));
+ app.footer.show(new Footer(viewOptions));
+
+ app.bindTo(viewOptions.collection, 'all', updateVisibility);
+ updateVisibility();
+ });
+
+ function updateVisibility() {
+ if (todoList.length === 0) {
+ app.main.$el.hide();
+ app.footer.$el.hide();
+ } else {
+ app.main.$el.show();
+ app.footer.$el.show();
+ }
+ }
+
+ vent.on('todoList:clear:completed',function(){
+ function destroy(todo) { todo.destroy(); }
+
+ todoList.getCompleted().forEach(destroy);
+ });
+
+ return app;
+
+ }
+);
@@ -0,0 +1,22 @@
+define(['backbone','models/Todo','lib/backbone-localStorage'],function(Backbone,Todo) {
+ 'use strict';
+
+ function isCompleted(todo) { return todo.get('completed'); }
+
+ var Todos = Backbone.Collection.extend({
+ model: Todo,
+ localStorage: new Backbone.LocalStorage('todos-backbone'),
+
+ getCompleted: function() {
+ return this.filter(isCompleted);
+ },
+ getActive: function() {
+ return this.reject(isCompleted);
+ },
+ comparator: function( todo ) {
+ return todo.get('created');
+ }
+ });
+
+ return Todos;
+});
@@ -0,0 +1,11 @@
+/*global define*/
+
+define(['vent'], function (vent) {
+ "use strict";
+
+ return {
+ setFilter : function(param) {
+ vent.trigger('todoList:filter', param.trim() || '');
+ }
+ };
+});
@@ -0,0 +1,136 @@
+/**
+ * Backbone localStorage Adapter
+ * https://github.com/jeromegn/Backbone.localStorage
+ */
+
+(function() {
+ // A simple module to replace `Backbone.sync` with *localStorage*-based
+ // persistence. Models are given GUIDS, and saved into a JSON object. Simple
+ // as that.
+
+ // Hold reference to Underscore.js and Backbone.js in the closure in order
+ // to make things work even if they are removed from the global namespace
+ var _ = this._;
+ var Backbone = this.Backbone;
+
+ // Generate four random hex digits.
+ function S4() {
+ return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
+ };
+
+ // Generate a pseudo-GUID by concatenating random hexadecimal.
+ function guid() {
+ return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
+ };
+
+ // Our Store is represented by a single JS object in *localStorage*. Create it
+ // with a meaningful name, like the name you'd give a table.
+ // window.Store is deprectated, use Backbone.LocalStorage instead
+ Backbone.LocalStorage = window.Store = function(name) {
+ this.name = name;
+ var store = this.localStorage().getItem(this.name);
+ this.records = (store && store.split(",")) || [];
+ };
+
+ _.extend(Backbone.LocalStorage.prototype, {
+
+ // Save the current state of the **Store** to *localStorage*.
+ save: function() {
+ this.localStorage().setItem(this.name, this.records.join(","));
+ },
+
+ // Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
+ // have an id of it's own.
+ create: function(model) {
+ if (!model.id) {
+ model.id = guid();
+ model.set(model.idAttribute, model.id);
+ }
+ this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
+ this.records.push(model.id.toString());
+ this.save();
+ return model.toJSON();
+ },
+
+ // Update a model by replacing its copy in `this.data`.
+ update: function(model) {
+ this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
+ if (!_.include(this.records, model.id.toString())) this.records.push(model.id.toString()); this.save();
+ return model.toJSON();
+ },
+
+ // Retrieve a model from `this.data` by id.
+ find: function(model) {
+ return JSON.parse(this.localStorage().getItem(this.name+"-"+model.id));
+ },
+
+ // Return the array of all models currently in storage.
+ findAll: function() {
+ return _(this.records).chain()
+ .map(function(id){return JSON.parse(this.localStorage().getItem(this.name+"-"+id));}, this)
+ .compact()
+ .value();
+ },
+
+ // Delete a model from `this.data`, returning it.
+ destroy: function(model) {
+ this.localStorage().removeItem(this.name+"-"+model.id);
+ this.records = _.reject(this.records, function(record_id){return record_id == model.id.toString();});
+ this.save();
+ return model;
+ },
+
+ localStorage: function() {
+ return localStorage;
+ }
+
+ });
+
+ // localSync delegate to the model or collection's
+ // *localStorage* property, which should be an instance of `Store`.
+ // window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead
+ Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options, error) {
+ var store = model.localStorage || model.collection.localStorage;
+
+ // Backwards compatibility with Backbone <= 0.3.3
+ if (typeof options == 'function') {
+ options = {
+ success: options,
+ error: error
+ };
+ }
+
+ var resp;
+
+ switch (method) {
+ case "read": resp = model.id != undefined ? store.find(model) : store.findAll(); break;
+ case "create": resp = store.create(model); break;
+ case "update": resp = store.update(model); break;
+ case "delete": resp = store.destroy(model); break;
+ }
+
+ if (resp) {
+ options.success(resp);
+ } else {
+ options.error("Record not found");
+ }
+ };
+
+ Backbone.ajaxSync = Backbone.sync;
+
+ Backbone.getSyncMethod = function(model) {
+ if(model.localStorage || (model.collection && model.collection.localStorage))
+ {
+ return Backbone.localSync;
+ }
+
+ return Backbone.ajaxSync;
+ };
+
+ // Override 'Backbone.sync' to default to localSync,
+ // the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
+ Backbone.sync = function(method, model, options, error) {
+ return Backbone.getSyncMethod(model).apply(this, [method, model, options, error]);
+ };
+
+})();
Oops, something went wrong. Retry.

0 comments on commit 4a049b5

Please sign in to comment.