我想要实现的是为所有需要在单独的js文件中(而不是在页面中呈现)内部生成的HTML模板存储HTML模板。JavaScript文件中的HTML模板...我做错了什么?
buildHtml
函数如何使其当前设置正常工作。当我坚持是什么,如果..我以前做的模板对象内的另一个变量说“输入3”和它的标记是一样的东西<div>(exact markup from commonInput)</div>
我试图使用它作为input 3 : '<div>' + this.commonInput + '</div>'
但事实证明,你不能指代对象的属性内部使用this
(explained here)。
我可以用完整的html创建'input3',但对于大的html块,这种方法不是很有用。
寻找
- 解决这一特定问题
- 原因,如果这种做法是一个坏主意
更好的替代品
$j(document).ready(function() { var namespace = window.namespace || {}; namespace.feature = namespace.appName || {}; namespace.feature.templates = { input1 : '<div>'+ '<p class="abc">Hey {username}</p>'+ '</div>', input2 : '<div>'+ '<div class="description">{description}</div>'+ '</div>', commonInput : '<div class="common">Common code</div>' }; namespace.feature.module = function() { var container = $j('#container'), t = namespace.feature.templates; var buildHtml = function(type) { var tmpHtml = t.input1 + t.commonInput + t.input2; container.append(tmpHtml); } var init = function() { buildHtml(); } return { init : init, }; }(); namespace.feature.module.init(); });
有你使用['_.template'(http://documentcloud.github.com/underscore/#template) – Raynos