我想要實現的是爲所有需要在單獨的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