mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-09 07:25:46 +01:00
got rid of all templates.prepare; pull blocks using templates.getBlock instead of parsing twice
This commit is contained in:
@@ -210,7 +210,7 @@ var ajaxify = {};
|
|||||||
|
|
||||||
data.relative_path = RELATIVE_PATH;
|
data.relative_path = RELATIVE_PATH;
|
||||||
|
|
||||||
templates.parse(tpl_url, data, function(err, template) {
|
templates.parse(tpl_url, data, function(template) {
|
||||||
translator.translate(template, function(translatedTemplate) {
|
translator.translate(template, function(translatedTemplate) {
|
||||||
$('#content').html(translatedTemplate);
|
$('#content').html(translatedTemplate);
|
||||||
|
|
||||||
@@ -235,6 +235,23 @@ var ajaxify = {};
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ajaxify.loadTemplate = function(template, callback) {
|
||||||
|
if (templates.cache[template]) {
|
||||||
|
callback(templates.cache[template]);
|
||||||
|
} else {
|
||||||
|
$.ajax({
|
||||||
|
url: RELATIVE_PATH + '/templates/' + template + '.tpl',
|
||||||
|
type: 'GET',
|
||||||
|
success: function(data) {
|
||||||
|
callback(data.toString());
|
||||||
|
},
|
||||||
|
error: function(error) {
|
||||||
|
throw new Error("Unable to load template: " + template + " (" + error.statusText + ")");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
ajaxify.variables = (function() {
|
ajaxify.variables = (function() {
|
||||||
var parsedVariables = {};
|
var parsedVariables = {};
|
||||||
|
|
||||||
@@ -297,9 +314,8 @@ var ajaxify = {};
|
|||||||
|
|
||||||
socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) {
|
socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) {
|
||||||
if (area.html()) {
|
if (area.html()) {
|
||||||
area.html(templates.parse(area.html(), {
|
area.html(templates.parse(area.html(), {widgets: renderedWidgets}))
|
||||||
widgets: renderedWidgets
|
.removeClass('hidden');
|
||||||
})).removeClass('hidden');
|
|
||||||
|
|
||||||
if (!renderedWidgets.length) {
|
if (!renderedWidgets.length) {
|
||||||
ajaxify.repositionNoWidgets();
|
ajaxify.repositionNoWidgets();
|
||||||
@@ -371,18 +387,7 @@ var ajaxify = {};
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
templates.registerLoader(function(template, callback) {
|
templates.registerLoader(ajaxify.loadTemplate);
|
||||||
$.ajax({
|
|
||||||
url: RELATIVE_PATH + '/templates/' + template + '.tpl',
|
|
||||||
type: 'GET',
|
|
||||||
success: function(data) {
|
|
||||||
callback(null, data.toString());
|
|
||||||
},
|
|
||||||
error: function(error) {
|
|
||||||
callback({message: error.statusText}, false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
$.when($.getJSON(RELATIVE_PATH + '/templates/config.json'), $.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) {
|
$.when($.getJSON(RELATIVE_PATH + '/templates/config.json'), $.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) {
|
||||||
templatesConfig = config_data[0];
|
templatesConfig = config_data[0];
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/* globals define, app, socket, templates, translator */
|
/* globals define, app, socket, ajaxify, templates, translator */
|
||||||
|
|
||||||
define(['forum/accountheader'], function(header) {
|
define(['forum/accountheader'], function(header) {
|
||||||
var AccountPosts = {},
|
var AccountPosts = {},
|
||||||
@@ -38,15 +38,10 @@ define(['forum/accountheader'], function(header) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onTopicsLoaded(posts) {
|
function onTopicsLoaded(posts) {
|
||||||
templates.preload_template('accountposts', function() {
|
ajaxify.loadTemplate('accountposts', function(accountposts) {
|
||||||
templates.accountposts.parse({posts: []});
|
var html = templates.parse(templates.getBlock(accountposts, 'posts'), {posts: posts});
|
||||||
|
|
||||||
var html = templates.prepare(templates.accountposts.blocks.posts).parse({
|
|
||||||
posts: posts
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
translator.translate(html, function(translatedHTML) {
|
||||||
|
|
||||||
$('#category-no-topics').remove();
|
$('#category-no-topics').remove();
|
||||||
|
|
||||||
html = $(translatedHTML);
|
html = $(translatedHTML);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
/*global define, socket, app, bootbox, templates, RELATIVE_PATH*/
|
/*global define, socket, app, bootbox, templates, ajaxify, RELATIVE_PATH*/
|
||||||
|
|
||||||
define(['uploader'], function(uploader) {
|
define(['uploader'], function(uploader) {
|
||||||
var Categories = {};
|
var Categories = {};
|
||||||
@@ -120,12 +120,9 @@ define(['uploader'], function(uploader) {
|
|||||||
timeout: 2000
|
timeout: 2000
|
||||||
});
|
});
|
||||||
|
|
||||||
templates.preload_template('admin/categories', function() {
|
ajaxify.loadTemplate('admin/categories', function(adminCategories) {
|
||||||
templates['admin/categories'].parse({categories:[]});
|
var html = $(templates.parse(templates.getBlock(adminCategories, 'categories'), {categories: [data]}));
|
||||||
var html = templates.prepare(templates['admin/categories'].blocks.categories).parse({
|
|
||||||
categories: [data]
|
|
||||||
});
|
|
||||||
html = $(html);
|
|
||||||
html.find('[data-name="bgColor"], [data-name="color"]').each(enableColorPicker);
|
html.find('[data-name="bgColor"], [data-name="color"]').each(enableColorPicker);
|
||||||
|
|
||||||
$('#entry-container').append(html);
|
$('#entry-container').append(html);
|
||||||
|
|||||||
@@ -193,14 +193,9 @@ define(function() {
|
|||||||
return app.alertError(err.message);
|
return app.alertError(err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.preload_template('admin/users', function() {
|
ajaxify.loadTemplate('admin/users', function(adminUsers) {
|
||||||
templates['admin/users'].parse({users:[]});
|
$('.users').html(templates.parse(templates.getBlock(adminUsers, 'users'), data));
|
||||||
var html = templates.prepare(templates['admin/users'].blocks.users).parse({
|
|
||||||
users: data.users
|
|
||||||
}),
|
|
||||||
userListEl = document.querySelector('.users');
|
|
||||||
|
|
||||||
userListEl.innerHTML = html;
|
|
||||||
$('.fa-spinner').addClass('none');
|
$('.fa-spinner').addClass('none');
|
||||||
|
|
||||||
if (data && data.users.length === 0) {
|
if (data && data.users.length === 0) {
|
||||||
@@ -226,14 +221,8 @@ define(function() {
|
|||||||
handleUserCreate();
|
handleUserCreate();
|
||||||
|
|
||||||
function onUsersLoaded(users) {
|
function onUsersLoaded(users) {
|
||||||
templates.preload_template('admin/users', function() {
|
ajaxify.loadTemplate('admin/users', function(adminUsers) {
|
||||||
templates['admin/users'].parse({users:[]});
|
$('#users-container').append($(templates.parse(templates.getBlock(adminUsers, 'users'), {users: users})));
|
||||||
var html = templates.prepare(templates['admin/users'].blocks.users).parse({
|
|
||||||
users: users
|
|
||||||
});
|
|
||||||
html = $(html);
|
|
||||||
$('#users-container').append(html);
|
|
||||||
|
|
||||||
updateUserBanButtons(html.find('.ban-btn'));
|
updateUserBanButtons(html.find('.ban-btn'));
|
||||||
updateUserAdminButtons(html.find('.admin-btn'));
|
updateUserAdminButtons(html.find('.admin-btn'));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -165,11 +165,9 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
|||||||
|
|
||||||
Category.onNewTopic = function(data) {
|
Category.onNewTopic = function(data) {
|
||||||
$(window).trigger('filter:categories.new_topic', data);
|
$(window).trigger('filter:categories.new_topic', data);
|
||||||
templates.preload_template('category', function() {
|
|
||||||
templates.category.parse({topics:[]});
|
ajaxify.loadTemplate('category', function(categoryTemplate) {
|
||||||
var html = templates.prepare(templates.category.blocks.topics).parse({
|
var html = templates.parse(templates.getBlock(categoryTemplate, 'topics'), {topics: [data]});
|
||||||
topics: [data]
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
translator.translate(html, function(translatedHTML) {
|
||||||
var topic = $(translatedHTML),
|
var topic = $(translatedHTML),
|
||||||
@@ -249,11 +247,8 @@ define(['composer', 'forum/pagination'], function(composer, pagination) {
|
|||||||
|
|
||||||
findInsertionPoint();
|
findInsertionPoint();
|
||||||
|
|
||||||
templates.preload_template('category', function() {
|
ajaxify.loadTemplate('category', function(categoryTemplate) {
|
||||||
templates.category.parse({topics:[]});
|
var html = templates.parse(templates.getBlock(categoryTemplate, 'topics'), {topics: topics});
|
||||||
var html = templates.prepare(templates.category.blocks.topics).parse({
|
|
||||||
topics: topics
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
translator.translate(html, function(translatedHTML) {
|
||||||
var container = $('#topics-container'),
|
var container = $('#topics-container'),
|
||||||
|
|||||||
@@ -33,14 +33,10 @@ define(['forum/accountheader'], function(header) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onTopicsLoaded(posts) {
|
function onTopicsLoaded(posts) {
|
||||||
templates.preload_template('favourites', function() {
|
ajaxify.loadTemplate('favourites', function(favouritesTemplate) {
|
||||||
templates['favourites'].parse({posts:[]});
|
var html = templates.parse(templates.getBlock(favouritesTemplate, 'posts'), {posts: posts});
|
||||||
var html = templates.prepare(templates['favourites'].blocks['posts']).parse({
|
|
||||||
posts: posts
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
translator.translate(html, function(translatedHTML) {
|
||||||
|
|
||||||
$('#category-no-topics').remove();
|
$('#category-no-topics').remove();
|
||||||
|
|
||||||
html = $(translatedHTML);
|
html = $(translatedHTML);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/* globals define, socket, app, templates, translator*/
|
/* globals define, socket, app, templates, translator, ajaxify*/
|
||||||
|
|
||||||
define(function() {
|
define(function() {
|
||||||
var home = {};
|
var home = {};
|
||||||
@@ -62,19 +62,8 @@ define(function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parseAndTranslate(posts, callback) {
|
function parseAndTranslate(posts, callback) {
|
||||||
templates.preload_template('home', function() {
|
ajaxify.loadTemplate('home', function(homeTemplate) {
|
||||||
|
var html = templates.parse(templates.getBlock(homeTemplate, 'categories.posts'), {categories: {posts: posts}});
|
||||||
templates.home.parse({
|
|
||||||
categories: {
|
|
||||||
posts: []
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var html = templates.prepare(templates.home.blocks['categories.posts']).parse({
|
|
||||||
categories: {
|
|
||||||
posts: posts
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
translator.translate(html, function(translatedHTML) {
|
||||||
translatedHTML = $(translatedHTML);
|
translatedHTML = $(translatedHTML);
|
||||||
|
|||||||
@@ -115,15 +115,11 @@ define(function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Recent.onTopicsLoaded = function(template, topics) {
|
Recent.onTopicsLoaded = function(templateName, topics) {
|
||||||
templates.preload_template(template, function() {
|
ajaxify.loadTemplate(templateName, function(template) {
|
||||||
templates[template].parse({topics:[]});
|
var html = templates.parse(templates.getBlock(template, 'topics'), {topics: topics});
|
||||||
var html = templates.prepare(templates[template].blocks['topics']).parse({
|
|
||||||
topics: topics
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translatedHTML) {
|
translator.translate(html, function(translatedHTML) {
|
||||||
|
|
||||||
$('#category-no-topics').remove();
|
$('#category-no-topics').remove();
|
||||||
|
|
||||||
html = $(translatedHTML);
|
html = $(translatedHTML);
|
||||||
|
|||||||
@@ -746,14 +746,12 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools'],
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parseAndTranslatePosts(data, callback) {
|
function parseAndTranslatePosts(data, callback) {
|
||||||
templates.preload_template('topic', function() {
|
ajaxify.loadTemplate('topic', function(topicTemplate) {
|
||||||
templates.topic.parse({posts: []});
|
var html = templates.parse(templates.getBlock(topicTemplate, 'posts'), data);
|
||||||
var html = templates.prepare(templates.topic.blocks.posts).parse(data);
|
|
||||||
translator.translate(html, callback);
|
translator.translate(html, callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function onNewPostsLoaded(html, posts) {
|
function onNewPostsLoaded(html, posts) {
|
||||||
for (var x = 0, numPosts = posts.length; x < numPosts; x++) {
|
for (var x = 0, numPosts = posts.length; x < numPosts; x++) {
|
||||||
toggle_mod_tools(posts[x].pid, posts[x].display_moderator_tools);
|
toggle_mod_tools(posts[x].pid, posts[x].display_moderator_tools);
|
||||||
|
|||||||
@@ -59,11 +59,8 @@ define(function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.preload_template('users', function() {
|
ajaxify.loadTemplate('users', function(usersTemplate) {
|
||||||
templates['users'].parse({users:[]});
|
var html = templates.parse(templates.getBlock(usersTemplate, 'topics'), data);
|
||||||
var html = templates.prepare(templates['users'].blocks['users']).parse({
|
|
||||||
users: data.users
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translated) {
|
translator.translate(html, function(translated) {
|
||||||
$('#users-container').html(translated);
|
$('#users-container').html(translated);
|
||||||
@@ -111,11 +108,8 @@ define(function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onUsersLoaded(users, emptyContainer) {
|
function onUsersLoaded(users, emptyContainer) {
|
||||||
templates.preload_template('users', function() {
|
ajaxify.loadTemplate('users', function(usersTemplate) {
|
||||||
templates['users'].parse({users:[]});
|
var html = templates.parse(templates.getBlock(usersTemplate, 'users'), {users: users});
|
||||||
var html = templates.prepare(templates['users'].blocks['users']).parse({
|
|
||||||
users: users
|
|
||||||
});
|
|
||||||
|
|
||||||
translator.translate(html, function(translated) {
|
translator.translate(html, function(translated) {
|
||||||
if(emptyContainer) {
|
if(emptyContainer) {
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
(function(module) {
|
(function(module) {
|
||||||
var templates = {},
|
var templates = {
|
||||||
|
cache: {}
|
||||||
|
},
|
||||||
helpers = {},
|
helpers = {},
|
||||||
globals = {},
|
globals = {},
|
||||||
cache = {},
|
|
||||||
loader,
|
loader,
|
||||||
originalObj;
|
originalObj;
|
||||||
|
|
||||||
@@ -22,16 +23,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (loader && callback) {
|
if (loader && callback) {
|
||||||
if (!cache[template]) {
|
if (!templates.cache[template]) {
|
||||||
loader(template, function(err, loaded) {
|
loader(template, function(err, loaded) {
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
cache[template] = loaded;
|
templates.cache[template] = loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(err, parse(loaded, obj, bind));
|
callback(err, parse(loaded, obj, bind));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(null, parse(cache[template], obj, bind));
|
callback(null, parse(templates.cache[template], obj, bind));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return parse(template, obj, bind);
|
return parse(template, obj, bind);
|
||||||
@@ -58,13 +59,13 @@
|
|||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
tpl = filename.replace(options.settings.views + '/', '');
|
tpl = filename.replace(options.settings.views + '/', '');
|
||||||
|
|
||||||
if (!cache[tpl]) {
|
if (!templates.cache[tpl]) {
|
||||||
fs.readFile(filename, function(err, html) {
|
fs.readFile(filename, function(err, html) {
|
||||||
cache[tpl] = html.toString();
|
templates.cache[tpl] = html.toString();
|
||||||
return fn(err, templates.parse(cache[tpl], options));
|
return fn(err, templates.parse(templates.cache[tpl], options));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
return fn(null, templates.parse(cache[tpl], options));
|
return fn(null, templates.parse(templates.cache[tpl], options));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user