Skip to content

Commit

Permalink
Removing field template compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
JedWatson committed Jan 15, 2015
1 parent ebc94e9 commit 08aa952
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 86 deletions.
65 changes: 0 additions & 65 deletions lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ function Field(list, path, options) {
process.exit(1);
}

// Set up templates
this.templateDir = fspath.normalize(options.templateDir || (__dirname + '../../templates/fields/' + this.type));

var defaultTemplates = {
form: this.templateDir + '/' + 'form.jade',
initial: this.templateDir + '/' + 'initial.jade'
};

this.templates = utils.options(defaultTemplates, this.options.templates);

// Add pre-save handler to the list if this field watches others
if (this.options.watch) {
this.list.schema.pre('save', this.getPreSaveWatcher());
Expand Down Expand Up @@ -361,58 +351,3 @@ Field.prototype.updateItem = function(item, data) {
Field.prototype.getValueFromData = function(data) {
return this.path in data ? data[this.path] : this._path.get(data);
};

/**
* Compiles a field template and caches it
*
* @api public
*/

Field.prototype.compile = function(type, callback) {

var templatePath = this.templates[type];

if (!compiledTemplates[templatePath]) {
fs.readFile(templatePath, 'utf8', function(err, file) {
if (!err){
compiledTemplates[templatePath] = jade.compile(file, {
filename: templatePath,
pretty: keystone.get('env') !== 'production'
});
}
if (callback) return callback();
});
} else if (callback) {
return callback();
}

};

/**
* Compiles a field template and caches it
*
* @api public
*/

Field.prototype.render = function(type, item, locals) {

var templatePath = this.templates[type];

// Compile the template synchronously if it hasn't already been compiled
if (!compiledTemplates[templatePath]) {

var file = fs.readFileSync(templatePath, 'utf8');

compiledTemplates[templatePath] = jade.compile(file, {
filename: templatePath,
pretty: keystone.get('env') !== 'production'
});

}

return compiledTemplates[templatePath](_.extend(locals || {}, {
field: this,
item: item
}));

};
37 changes: 16 additions & 21 deletions routes/views/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,23 @@ exports = module.exports = function(req, res) {
download_link += '?' + downloadParams;
}

var compileFields = function(item, callback) { item.compile('initial', callback); };
keystone.render(req, res, 'list', _.extend(viewLocals, {
section: keystone.nav.by.list[req.list.key] || {},
title: 'Keystone: ' + req.list.plural,
page: 'list',
link_to: link_to,
download_link: download_link,
list: req.list,
sort: sort,
filters: cleanFilters,
search: req.query.search,
columns: columns,
colPaths: _.pluck(columns, 'path'),
items: items,
submitted: req.body || {},
query: req.query
}));

async.eachSeries(req.list.initialFields, compileFields , function() {

keystone.render(req, res, 'list', _.extend(viewLocals, {
section: keystone.nav.by.list[req.list.key] || {},
title: 'Keystone: ' + req.list.plural,
page: 'list',
link_to: link_to,
download_link: download_link,
list: req.list,
sort: sort,
filters: cleanFilters,
search: req.query.search,
columns: columns,
colPaths: _.pluck(columns, 'path'),
items: items,
submitted: req.body || {},
query: req.query
}));

});
});

};
Expand Down

0 comments on commit 08aa952

Please sign in to comment.