Skip to content

Commit

Permalink
Speed up rendering of subtemplates.
Browse files Browse the repository at this point in the history
  • Loading branch information
rottmann committed Nov 28, 2014
1 parent 19a5988 commit 7a78c8a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function createOutputFiles(blocks, packageInfos) {

app.log.debug('write js file: ' + options.dest + 'api_data.js');
if( ! options.simulate)
fs.writeFileSync(options.dest + './api_data.js', 'define({ api: ' + apiData + ' });');
fs.writeFileSync(options.dest + './api_data.js', 'define({ "api": ' + apiData + ' });');

// Write api_project
app.log.debug('write json file: ' + options.dest + 'api_project.json');
Expand Down
21 changes: 20 additions & 1 deletion template/handlebars_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ define([
'handlebars',
'diffMatchPatch'
], function(locale, Handlebars, DiffMatchPatch) {

/**
* start/stop timer for simple performance check.
*/
var timer;
Handlebars.registerHelper('startTimer', function(text) {
timer = new Date();
return '';
});

Handlebars.registerHelper('stopTimer', function(text) {
console.log(new Date() - timer);
return '';
});

/**
* Return localized Text.
* @param string text
Expand Down Expand Up @@ -81,8 +96,12 @@ define([
/**
*
*/
var templateCache = {};
Handlebars.registerHelper('subTemplate', function(name, sourceContext) {
var template = Handlebars.compile($('#template-' + name).html());
if ( ! templateCache[name])
templateCache[name] = Handlebars.compile($('#template-' + name).html());

var template = templateCache[name];
var templateContext = $.extend({}, this, sourceContext.hash);
return new Handlebars.SafeString( template(templateContext) );
});
Expand Down
19 changes: 9 additions & 10 deletions template/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require([
'pathToRegexp'
], function($, _, locale, Handlebars, apiProject, apiData, prettyPrint, sampleRequest) {

// Load google web fonts.
// load google web fonts
loadGoogleFontCss();

var api = apiData.api;
Expand Down Expand Up @@ -81,13 +81,12 @@ require([
//
// Data transform
//

// Grouped by group
// grouped by group
var apiByGroup = _.groupBy(api, function(entry) {
return entry.group;
});

// Grouped by group and name
// grouped by group and name
var apiByGroupAndName = {};
$.each(apiByGroup, function(index, entries) {
apiByGroupAndName[index] = _.groupBy(entries, function(entry) {
Expand Down Expand Up @@ -157,7 +156,7 @@ require([
apiVersions.reverse();

//
// Create Navigationlist
// create Navigationlist
//
var nav = [];
apiGroups.forEach(function(group) {
Expand Down Expand Up @@ -246,6 +245,7 @@ require([
// Render Sections and Articles
//
var articleVersions = {};
var content = '';
apiGroups.forEach(function(groupEntry) {
var articles = [];
var oldName = '';
Expand All @@ -254,7 +254,7 @@ require([
var description = '';
articleVersions[groupEntry] = {};

// render all Articls of a group
// render all articles of a group
api.forEach(function(entry) {
if(groupEntry === entry.group) {
if (oldName !== entry.name) {
Expand All @@ -271,9 +271,7 @@ require([
article: entry,
versions: articleVersions[entry.group][entry.name]
};
}
else
{
} else {
fields = {
article: entry,
hidden: true,
Expand Down Expand Up @@ -310,8 +308,9 @@ require([
description: description,
articles: articles
};
$('#sections').append( templateSections(fields) );
content += templateSections(fields);
});
$('#sections').append( content );

// Bootstrap Scrollspy
var $scrollSpy = $(this).scrollspy({ target: '#scrollingNav', offset: 25 });
Expand Down

0 comments on commit 7a78c8a

Please sign in to comment.