Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2640E10643 for ; Wed, 8 May 2013 15:13:12 +0000 (UTC) Received: (qmail 46023 invoked by uid 500); 8 May 2013 15:13:12 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 45870 invoked by uid 500); 8 May 2013 15:13:11 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 45844 invoked by uid 99); 8 May 2013 15:13:11 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 May 2013 15:13:11 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 1782B889BD4; Wed, 8 May 2013 15:13:11 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: garren@apache.org To: commits@couchdb.apache.org Date: Wed, 08 May 2013 15:13:11 -0000 Message-Id: <83314310fda04603b211bff30a7ce3d5@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/2] git commit: updated refs/heads/route-events to 9f59c62 Updated Branches: refs/heads/route-events 82de43fef -> 9f59c6264 (forced update) Route Events working with simplified API Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/d17be670 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/d17be670 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/d17be670 Branch: refs/heads/route-events Commit: d17be6703f8d71fd5934946de049509d22bbdfa1 Parents: 366535e Author: Garren Smith Authored: Tue May 7 14:45:49 2013 +0200 Committer: Garren Smith Committed: Tue May 7 15:31:13 2013 +0200 ---------------------------------------------------------------------- src/fauxton/app/addons/config/routes.js | 6 +- src/fauxton/app/addons/logs/routes.js | 8 +- src/fauxton/app/addons/stats/routes.js | 8 +- src/fauxton/app/api.js | 47 ++-- src/fauxton/app/modules/databases/routes.js | 16 +- src/fauxton/app/modules/databases/views.js | 3 +- src/fauxton/app/modules/documents/routes.js | 185 +++------------ src/fauxton/app/modules/documents/views.js | 1 - src/fauxton/app/modules/fauxton/base.js | 4 +- src/fauxton/app/router.js | 70 +----- .../templates/documents/doc_field_editor_tabs.html | 4 +- .../app/templates/documents/index_menu_item.html | 2 +- src/fauxton/app/templates/documents/sidebar.html | 6 +- src/fauxton/app/templates/fauxton/pagination.html | 10 +- 14 files changed, 96 insertions(+), 274 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/addons/config/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/config/routes.js b/src/fauxton/app/addons/config/routes.js index 495e3a9..42934a8 100644 --- a/src/fauxton/app/addons/config/routes.js +++ b/src/fauxton/app/addons/config/routes.js @@ -36,9 +36,9 @@ function(app, FauxtonAPI, Config) { this.configs.url(); }, - routes: ["_config"], - - defaultRoute: "config", + routes: { + "_config": "config" + }, config: function () { this.setView("#dashboard-content", new Config.View({collection: this.configs})); http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/addons/logs/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/logs/routes.js b/src/fauxton/app/addons/logs/routes.js index 9bd8f65..1ce260c 100644 --- a/src/fauxton/app/addons/logs/routes.js +++ b/src/fauxton/app/addons/logs/routes.js @@ -28,9 +28,9 @@ function(app, FauxtonAPI, Log) { {"name": "Logs", "link": "_log"} ], - routes: ["_log"], - - defaultRoute: "showLog", + routes: { + "_log": "showLog" + }, apiUrl: function() { return this.logs.url(); @@ -41,7 +41,7 @@ function(app, FauxtonAPI, Log) { this.setView("#sidebar-content", new Log.Views.FilterView({})); }, - showLog: function (event) { + showLog: function () { this.setView("#dashboard-content", new Log.Views.View({collection: this.logs})); }, http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/addons/stats/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/stats/routes.js b/src/fauxton/app/addons/stats/routes.js index 7ad2a1f..32017c3 100644 --- a/src/fauxton/app/addons/stats/routes.js +++ b/src/fauxton/app/addons/stats/routes.js @@ -21,8 +21,10 @@ function(app, FauxtonAPI, Stats) { var StatsRouteObject = FauxtonAPI.RouteObject.extend({ layout: "with_sidebar", - routes: ["stats", "_stats"], - defaultRoute: "showStats", + routes: { + "stats":"showStats", + "_stats": "showStats" + }, initialize: function () { this.stats = new Stats.Collection(); @@ -33,7 +35,7 @@ function(app, FauxtonAPI, Stats) { }, - showStats: function (event) { + showStats: function () { this.setView("#dashboard-content", new Views.Statistics({ collection: this.stats })); http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/api.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/api.js b/src/fauxton/app/api.js index 6591daf..1c0b8f8 100644 --- a/src/fauxton/app/api.js +++ b/src/fauxton/app/api.js @@ -108,25 +108,6 @@ function(app, Fauxton) { } }); - // Not needed, could be removed. - 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; @@ -150,7 +131,6 @@ function(app, Fauxton) { layout: "with_sidebar", apiUrl: null, renderedState: false, - currTab: "databases", establish: function() {}, route: function() {}, initialize: function() {} @@ -161,13 +141,15 @@ function(app, Fauxton) { // immediately if its already done, but this way the RouteObject.route // function can rebuild the deferred as needed render: function(route, masterLayout, args) { - this.route.call(this, route, args); this.renderWith.apply(this, Array.prototype.slice.call(arguments)); }, renderWith: function(route, masterLayout, args) { var routeObject = this; + // Can look at replacing this with events eg beforeRender, afterRender + this.route.call(this, route, args); + // Only want to redo the template if its a full render if (!this.renderedState) { masterLayout.setTemplate(this.layout); @@ -184,10 +166,10 @@ function(app, Fauxton) { $.when.apply(this, this.establish()).done(function(resp) { _.each(routeObject.getViews(), function(view, selector) { - if(view.hasRendered()) { console.log('view been rendered'); return; } + if(view.hasRendered()) { return; } masterLayout.setView(selector, view); - console.log('set and render ', selector, view); + console.log('SET SELECTOR AND RENDER ', selector, view); $.when.apply(null, view.establish()).then(function(resp) { masterLayout.renderView(selector); @@ -248,6 +230,25 @@ function(app, Fauxton) { getViews: function() { return this.views; + }, + + // Could move getRouteUrls into the Constructor function and so it defines the urls + // only once. This would give us a small speed up. + getRouteUrls: function () { + return _.keys(this.get('routes')); + }, + + hasRoute: function (route) { + if (this.getRouteUrls().indexOf(route) > -1) { + return true; + } + + return false; + }, + + routeCallback: function (route) { + var routes = this.get('routes'); + return this[routes[route]]; } }); http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/modules/databases/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/databases/routes.js b/src/fauxton/app/modules/databases/routes.js index f1be372..104d876 100644 --- a/src/fauxton/app/modules/databases/routes.js +++ b/src/fauxton/app/modules/databases/routes.js @@ -30,14 +30,12 @@ function(app, FauxtonAPI, Databases, Views) { {"name": "Databases", "link": "/_all_dbs"} ], - events: { - "route:all_databases": "allDatabases" + routes: { + "": "allDatabases", + "index.html": "allDatabases", + "_all_dbs(:params)": "allDatabases" }, - defaultRoute: "allDatabases", - - routes: ["", "index.html", "_all_dbs(:params)"], - apiUrl: function() { return this.databases.url(); }, @@ -51,10 +49,8 @@ function(app, FauxtonAPI, Databases, Views) { })); }, - allDatabases: function(event) { - event = event || {}; - - var params = app.getParams(event.attr), + allDatabases: function() { + var params = app.getParams(), dbPage = params.page; this.databasesView = this.setView("#dashboard-content", new Views.List({ http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/modules/databases/views.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/databases/views.js b/src/fauxton/app/modules/databases/views.js index 23145fe..c1b9bb7 100644 --- a/src/fauxton/app/modules/databases/views.js +++ b/src/fauxton/app/modules/databases/views.js @@ -83,8 +83,7 @@ function(app, Fauxton, FauxtonAPI) { total: this.collection.length, urlFun: function(page) { return "#/_all_dbs?page=" + page; - }, - routeEvent: "all_databases" + } })); }, http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/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 d57487f..103d904 100644 --- a/src/fauxton/app/modules/documents/routes.js +++ b/src/fauxton/app/modules/documents/routes.js @@ -45,23 +45,12 @@ function(app, FauxtonAPI, Documents, Databases) { }, - routes: function() { - return _.keys(this.selectedRoutes); - }, - - selectedRoutes: { + routes: { "database/:database/:doc/field_editor": "field_editor", "database/:database/:doc/code_editor": "code_editor", "database/:database/:doc": "code_editor" }, - events: { - "route:field_editor": "field_editor", - "route:code_editor": "code_editor" - }, - - defaultRoute: "code_editor", - crumbs: function() { return [ {"name": "Databases", "link": "/_all_dbs"}, @@ -89,7 +78,7 @@ function(app, FauxtonAPI, Documents, Databases) { } }); - var newViewEditorCallback = function(databaseName) { + /*var newViewEditorCallback = function(databaseName) { var data = { database: new Databases.Model({id:databaseName}) }; @@ -128,13 +117,13 @@ function(app, FauxtonAPI, Documents, Databases) { apiUrl: data.database.url() }; - }; + };*/ // HACK: this kind of works // Basically need a way to share state between different routes, for // instance making a new doc won't work for switching back and forth // between code and field editors - var newDocCodeEditorCallback = function(databaseName) { + /*var newDocCodeEditorCallback = function(databaseName) { var data = { database: new Databases.Model({id:databaseName}), doc: new Documents.NewDoc(), @@ -176,21 +165,18 @@ function(app, FauxtonAPI, Documents, Databases) { apiUrl: data.doc.url() }; - }; + };*/ var DocumentsRouteObject = FauxtonAPI.RouteObject.extend({ layout: "with_tabs_sidebar", - events: { - "route:all_docs": "allDocs", - "route:all_design_docs": "allDesignDocs", - "route:view_fn": "viewFn", - "route:new_view": "newViewEditor" + routes: { + "database/:database/_all_docs(:extra)": "allDocs", + "database/:database/_design/:ddoc/_view/:view": "viewFn", + "database/:database/new_view": "newViewEditor" }, - defaultRoute: "allDocs", - initialize: function (options) { var docOptions = app.getParams(); docOptions.include_docs = true; @@ -218,69 +204,35 @@ function(app, FauxtonAPI, Documents, Databases) { })); }, - crumbs: function () { - return [ - {"name": "Databases", "link": "/_all_dbs"}, - {"name": this.data.database.id, "link": Databases.databaseUrl(this.data.database)} - ]; - }, - allDocs: function(event) { - var docOptions; + allDocs: function(databaseName, options) { + var docOptions = app.getParams(options); - docOptions = app.getParams(event.attr); docOptions.include_docs = true; - this.data.database.buildAllDocs(docOptions); - this.sidebar.setSelectedTab('all-docs'); - - this.documentsView = this.setView("#dashboard-content", new Documents.Views.AllDocsList({ - collection: this.data.database.allDocs - })); - }, - allDesignDocs: function(event) { - var docOptions = app.getParams(event.attr); - docOptions.include_docs = true; - - this.data.database.buildAllDocs(docOptions); - this.sidebar.setSelectedTab('design-docs'); + if (docOptions.startkey && docOptions.startkey.indexOf('_design') > -1) { + this.sidebar.setSelectedTab('design-docs'); + } else { + this.sidebar.setSelectedTab('all-docs'); + } this.documentsView = this.setView("#dashboard-content", new Documents.Views.AllDocsList({ collection: this.data.database.allDocs })); - }, - - route: function (route, routeArgs) { - console.log('ROUTE ARG ', arguments); - - if (route === 'database/:database/_design/:ddoc/_view/:view') { - if (!this.docCrumbs && !this.docApiUrl) { - // Save the old crumbs and API. Easier to do it here than in each event - this.docCrumbs = this.crumbs; - this.docApiUrl = this.apiUrl; - } + this.crumbs = [ + {"name": "Databases", "link": "/_all_dbs"}, + {"name": this.data.database.id, "link": Databases.databaseUrl(this.data.database)} + ]; - this.routeArgs = { - designDoc: routeArgs[1], - view: routeArgs[2].replace(/\?.*$/,'') - }; - } else { - this.routeArgs = {}; - if (this.docCrumbs && this.docApiUrl) { - this.crumbs = this.docCrumbs; - this.docApiUrl = this.apiUrl; - } - } + this.apiUrl = this.data.database.allDocs.url(); }, - viewFn: function (event) { - var view = this.routeArgs.view, - ddoc = this.routeArgs.designDoc, - params = app.getParams(event.attr); + viewFn: function (databaseName, ddoc, view) { + var params = app.getParams(); - console.log('PARAMS', params); + view = view.replace(/\?.*$/,''); this.data.indexedDocs = new Documents.IndexCollection(null, { database: this.data.database, @@ -303,6 +255,8 @@ function(app, FauxtonAPI, Documents, Databases) { params: params })); + this.sidebar.setSelectedTab(ddoc + '_' + view); + this.crumbs = function () { return [ {"name": "Databases", "link": "/_all_dbs"}, @@ -311,7 +265,6 @@ function(app, FauxtonAPI, Documents, Databases) { ]; }; - // TODO: change to view URL this.apiUrl = this.data.indexedDocs.url(); }, @@ -322,13 +275,10 @@ function(app, FauxtonAPI, Documents, Databases) { ddocs: this.data.designDocs })); - }, - - routes: ["database/:database/_all_docs(:extra)", "database/:database/_design/:ddoc/_view/:view", "database/:database/new_view"], + this.sidebar.setSelectedTab('new-view'); - apiUrl: function() { - return this.data.database.allDocs.url(); } + }); @@ -344,14 +294,10 @@ function(app, FauxtonAPI, Documents, Databases) { ]; }, - routes: ["database/:database/_changes(:params)"], - - events: { - "route:_changes": "changes" + routes: { + "database/:database/_changes(:params)": "changes" }, - defaultRoute: "changes", - initialize: function (options) { this.databaseName = options[0]; this.database = new Databases.Model({id: this.databaseName}); @@ -385,78 +331,9 @@ function(app, FauxtonAPI, Documents, Databases) { return codeEditorCallback(database, docID); }, - - "database/:database/new": newDocCodeEditorCallback, "database/:database/new_view": newViewEditorCallback, - - // TODO: fix optional search params - // Can't get ":view(?*search)" to work - // However ":view?*search" does work - //"database/:database/_design/:ddoc/_view/:view(\?*options)": function(databaseName, ddoc, view, options) { - "database/:database/_design/:ddoc/_view/:view": function(databaseName, ddoc, view, options) { -// hack around backbone router limitations -view = view.replace(/\?.*$/,''); -var params = app.getParams(); -var data = { -database: new Databases.Model({id:databaseName}) -}; - -data.indexedDocs = new Documents.IndexCollection(null, { -database: data.database, -design: ddoc, -view: view, -params: params -}); - -data.designDocs = new Documents.AllDocs(null, { -database: data.database, -params: {startkey: '"_design"', -endkey: '"_design1"', -include_docs: true} -}); - -var ddocInfo = { -id: "_design/" + ddoc, -currView: view, -designDocs: data.designDocs -}; - -return { -layout: "with_tabs_sidebar", - -data: data, - // TODO: change dashboard-content -views: { -"#dashboard-content": new Documents.Views.AllDocsList({ -collection: data.indexedDocs, -nestedView: Documents.Views.Row, -viewList: true, -ddocInfo: ddocInfo, -params: params -}), - -"#sidebar-content": new Documents.Views.Sidebar({ -collection: data.designDocs, -ddocInfo: ddocInfo -}), - -"#tabs": new Documents.Views.Tabs({ -collection: data.designDocs, -database: data.database -}) -}, - -crumbs: [ -{"name": "Databases", "link": "/_all_dbs"}, -{"name": data.database.id, "link": Databases.databaseUrl(data.database)}, -{"name": ddoc + "/" + view, "link": data.indexedDocs.url()} -], - // TODO: change to view URL - apiUrl: data.indexedDocs.url() - }; -} -};*/ + };*/ Documents.RouteObjects = [DocEditorRouteObject, DocumentsRouteObject, ChangesRouteObject]; http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/modules/documents/views.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/documents/views.js b/src/fauxton/app/modules/documents/views.js index 6ac9b44..17bf9af 100644 --- a/src/fauxton/app/modules/documents/views.js +++ b/src/fauxton/app/modules/documents/views.js @@ -396,7 +396,6 @@ function(app, FauxtonAPI, Codemirror, JSHint) { var fragment = window.location.hash.replace(/\?.*$/, ''); fragment = fragment + '?' + $.param(params); FauxtonAPI.navigate(fragment); - FauxtonAPI.triggerRouteEvent('view_fn', params); }, updateFilters: function(event) { http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/modules/fauxton/base.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/modules/fauxton/base.js b/src/fauxton/app/modules/fauxton/base.js index 92adb76..32ab6c9 100644 --- a/src/fauxton/app/modules/fauxton/base.js +++ b/src/fauxton/app/modules/fauxton/base.js @@ -137,7 +137,6 @@ function(app, Backbone) { this.total = options.total; this.totalPages = Math.ceil(this.total / this.perPage); this.urlFun = options.urlFun; - this.routeEvent = options.routeEvent; }, @@ -147,8 +146,7 @@ function(app, Backbone) { perPage: this.perPage, total: this.total, totalPages: this.totalPages, - urlFun: this.urlFun, - routeEvent: this.routeEvent + urlFun: this.urlFun }; } }); http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/router.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/router.js b/src/fauxton/app/router.js index 09b2338..45cc8b2 100644 --- a/src/fauxton/app/router.js +++ b/src/fauxton/app/router.js @@ -44,80 +44,30 @@ define([ function(req, app, Initialize, FauxtonAPI, Fauxton, Layout, Databases, Documents, Pouch, LoadAddons) { - var defaultLayout = 'with_sidebar'; // TODO: auto generate this list if possible var modules = [Databases, Documents]; - var generateRoute = function(settingsGenerator, route) { - return function() { - var boundRoute = route; - var settings = settingsGenerator.apply(null, arguments); - var layout = settings.layout || defaultLayout; - var establish = settings.establish || function() { return null; }; - var masterLayout = this.masterLayout; - - console.log("Settings generator for: ", layout, settings); - - masterLayout.setTemplate(layout); - masterLayout.clearBreadcrumbs(); - - if (settings.crumbs) { - masterLayout.setBreadcrumbs(new Fauxton.Breadcrumbs({ - crumbs: settings.crumbs - })); - } - - $.when.apply(null, establish()).done(function(resp) { - _.each(settings.views, function(view, selector) { - masterLayout.setView(selector, view); - - $.when.apply(null, view.establish()).then(function(resp) { - masterLayout.renderView(selector); - }, function(resp) { - view.establishError = { - error: true, - reason: resp - }; - masterLayout.renderView(selector); - }); - - var hooks = masterLayout.hooks[selector]; - - if(hooks){ - _.each(hooks, function(hook){ - if (_.any(hook.routes, function(route){return route == boundRoute;})){ - hook.callback(view); - } - }); - } - }); - }); - - if (settings.apiUrl) this.apiBar.update(settings.apiUrl); - }; - }; - var Router = app.router = Backbone.Router.extend({ routes: {}, addModuleRouteObject: function(RouteObject) { var self = this; - var masterLayout = this.masterLayout; + var masterLayout = this.masterLayout, + routeUrls = RouteObject.prototype.getRouteUrls(); - _.each(RouteObject.prototype.get('routes'), function(route) { + _.each(routeUrls, function(route) { this.route(route, route.toString(), function() { var args = Array.prototype.slice.call(arguments); - if (self.activeRouteObject && self.activeRouteObject.get('routes').indexOf(route) > -1) { - //Don't need to do anything here as this route has been initialised - self.activeRouteObject.route.call(self.activeRouteObject, route, args); - console.log('Avoiding Route creation'); - return; + if (!self.activeRouteObject || !self.activeRouteObject.hasRoute(route)) { + self.activeRouteObject = new RouteObject(args); } - self.activeRouteObject = new RouteObject(args); - self.activeRouteObject[self.activeRouteObject.defaultRoute].apply(self.activeRouteObject, args); - self.activeRouteObject.render(route, masterLayout, args); + var routeObject = self.activeRouteObject, + routeCallback = routeObject.routeCallback(route); + + routeCallback.apply(routeObject, args); + routeObject.render(route, masterLayout, args); }); }, this); }, http://git-wip-us.apache.org/repos/asf/couchdb/blob/d17be670/src/fauxton/app/templates/documents/doc_field_editor_tabs.html ---------------------------------------------------------------------- diff --git a/src/fauxton/app/templates/documents/doc_field_editor_tabs.html b/src/fauxton/app/templates/documents/doc_field_editor_tabs.html index b094e0e..41bbe40 100644 --- a/src/fauxton/app/templates/documents/doc_field_editor_tabs.html +++ b/src/fauxton/app/templates/documents/doc_field_editor_tabs.html @@ -13,8 +13,8 @@ the License. -->