/cdnsrc/js/controller/DocumentController.js
JavaScript | 143 lines | 62 code | 12 blank | 69 comment | 3 complexity | eb86a5e8ef61a8d68df88b49367f9afc MD5 | raw file
Possible License(s): Apache-2.0
- /**
- Aaron Greenlee
- http://aarongreenlee.com/
-
- This work is licensed under a Creative Commons Attribution-Share-Alike 3.0
- Unported License.
-
- // Original Info -----------------------------------------------------------
- Author : Aaron Greenlee
- Created : 12/19/2010 9:47:00 AM
-
- @fileoverview Example of a Backbone.js Controller.
- @suppress {deprecated}
- // Modifications :----------------------------------------------------------
-
- */
- // ---------------------------------------------------------------------
- var DocumentController = Backbone.Controller.extend
- ({
- // -----------------------------------------------------------------
- // Controller Configuration
- // -----------------------------------------------------------------
- // A backbone.js controller listens to changes within the location
- // hash and executes the function (aka action) you associate to
- // the route.
- routes :
- {
- // When we first start the application, we will not have a hash
- // so we define our first route to be empty.
- "" : "welcome"
- // You could clear the hash; however, I prefer to use #home and
- // simply associate it with the same action as our default
- // route.
- ,"!/home" : "welcome"
- // Backbone uses a similar convention to the ColdBox
- // Framework or or Ruby on Rails. You can pass variables which
- // are represented by ':foo'. When the associated actions is
- // executed by backbone.js the 'message' will be passed as an
- // argument to the function.
- ,"!/extra/:message" : "extraAction"
- // For example, we want to show the detail view of a document.
- // So, we will make a route for that! Notice the "!" prefix.
- // I want to tell Googlebot to index this AJAX content. There
- // is more to it than just prefixing the hash with an
- // exclamation mark, but, you get the idea.
- ,"!/document/:documentNumber" : "detail"
- }
-
- // Constructor
- ,initialize: function(options)
- {
- // Create a reference to our Document Collection
- this.DocumentCollection = new DocumentCollection();
-
- // Create a reference to our DocumentAreaView
- this.DocumentArea = new DocumentAreaView
- ({
- // We will pass our DocumentCollection to our area view.
- // Our area view is responsible for 'many' models and can
- // listen to events such as 'sort', 'delete all' or perhaps
- // 'create new'.
- model: this.DocumentCollection
- });
-
- // Explicitly add a document to inform people who glance at
- // this backbone.js tutorial that the other models are randomly
- // generated and the text won't make much sene.
- var d = new Date();
- var instance =
- {
- id: 1
- ,summary: 'Example Document. Other documents will not make\
- sense as they are randomly generated from a list of words.'
- ,created: d.getMonth()
- + '/' + d.getDate()
- + '/' + d.getFullYear()
- ,modified: d.getMonth()
- + '/' + d.getDate()
- + '/' + d.getFullYear()
- ,description: 'This is an example document. The rest of the\
- the documents are randomly generated. The other documents\
- will not make much sense.'
- ,author: 'Aaron Greenlee'
- };
-
- this.DocumentCollection.add( new DocumentModel(instance) );
-
- // For demonstration purposes, I am now going to create some
- // example documents so we have something to see.
- var n = (Math.floor(Math.random() * 14) + 2);
- // Quickly ensure we have at least six
- n = (n < 6) ? 6 : n;
- // Make our documents
- for (var i = 2; i < n; i++)
- {
- this.DocumentCollection.addRandomDocument(i);
- }
-
- // Render our current application view. You may consider this:
- // * A Master View for the current activity.
- // * A Layout for the current activity.
- // * The area of the screen that holds all the individual
- // views and listens for 'global' or 'many-model' events.
- this.DocumentArea.render();
-
- return this;
- }
-
- // -----------------------------------------------------------------
- // Actions
- // -----------------------------------------------------------------
- // Our default home action.
- // @route: "", "home"
- ,welcome : function ()
- {
- return this;
- }
-
- // An example of an action that accepts URL variables.
- // @route: "extra/message";
- ,extraAction : function (message)
- {
- // I simply want to demonstrate the ability to do something with
- // URL variables. The view is not changing.
- alert(message);
- return this;
- }
-
- // @route document/:cid
- ,detail : function (documentNumber)
- {
- var DocumentModel = this.DocumentCollection.get(documentNumber);
-
- // Because we have random numbers, on a reload, this model may
- // no longer exist.
- if (typeof DocumentModel != 'object')
- {
- window.location.hash = 'home';
- }
- this.DocumentArea.showDocument(DocumentModel);
- }
- });