add callback chain when a route is called
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/10a69fb2 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/10a69fb2 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/10a69fb2 Branch: refs/heads/route-events Commit: 10a69fb2c1dbdd254f3caf67ab4dbba85bee69be Parents: f202373 Author: Garren Smith <[email protected]> Authored: Mon Apr 29 16:46:40 2013 +0200 Committer: Garren Smith <[email protected]> Committed: Thu May 9 09:59:59 2013 +0200 ---------------------------------------------------------------------- src/fauxton/app/api.js | 18 +++++++++++++++ src/fauxton/app/modules/documents/routes.js | 4 +++ src/fauxton/app/router.js | 25 +++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/10a69fb2/src/fauxton/app/api.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js index a7496f4..de0844c 100644 --- a/src/fauxton/app/api.js +++ b/src/fauxton/app/api.js @@ -96,6 +96,24 @@ function(app, Fauxton) { } }); + FauxtonAPI.routeCallChain = { + callChain: {}, + + registerBeforeRoute: function (name, fn) { + this.callChain[name] = fn; + }, + + unregisterBeforeRoute: function (name) { + delete callChain[name]; + }, + + run: function () { + var callChainDeferreds = _.map(this.callChain, function (cb) { return cb(); }); + return $.when(null, callChainDeferreds ); + } + }; + + FauxtonAPI.RouteObject = function(options) { this._options = options; http://git-wip-us.apache.org/repos/asf/couchdb/blob/10a69fb2/src/fauxton/app/modules/documents/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/documents/routes.js b/src/fauxton/app/modules/documents/routes.js index 519ad7f..78af28e 100644 --- a/src/fauxton/app/modules/documents/routes.js +++ b/src/fauxton/app/modules/documents/routes.js @@ -182,6 +182,10 @@ function(app, FauxtonAPI, Documents, Databases) { var DocumentsRouteObject = FauxtonAPI.RouteObject.extend({ layout: "with_tabs_sidebar", + initialize: function () { + + }, + events: { "route:all_docs": "allDocs", "route:all_design_docs": "allDesignDocs" http://git-wip-us.apache.org/repos/asf/couchdb/blob/10a69fb2/src/fauxton/app/router.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js index dc04be3..2bcd071 100644 --- a/src/fauxton/app/router.js +++ b/src/fauxton/app/router.js @@ -97,6 +97,21 @@ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, Databases, Documents }; }; + FauxtonAPI.routeCallChain.registerBeforeRoute('no_deferred', function () { + console.log('This is a before route callback with no deferred.'); + }); + + FauxtonAPI.routeCallChain.registerBeforeRoute('deferred', function () { + var deferred = $.Deferred(); + + setTimeout(function () { + console.log('hi this is a delayed called before each route.'); + deferred.resolve(); + }, 10); + + return deferred; + }); + var Router = app.router = Backbone.Router.extend({ routes: {}, @@ -110,14 +125,22 @@ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, Databases, Documents addModuleRouteObject: function(routeObject) { var self = this; var masterLayout = this.masterLayout; + _.each(routeObject.get('routes'), function(route) { this.route(route, route.toString(), function() { + var args = arguments; + if (self.activeRouteObject && routeObject !== self.activeRouteObject) { self.activeRouteObject.renderedState = false; self.activeRouteObject.clearViews(); } - routeObject.render(route, masterLayout, Array.prototype.slice.call(arguments)); + FauxtonAPI.routeCallChain.run().then(function () { + routeObject.initialize(); + routeObject.render(route, masterLayout, Array.prototype.slice.call(args)); + }); + + self.activeRouteObject = routeObject; }); }, this);
