2013-04-09 28 views
2

我试图为即将推出的项目选择一个JS模板引擎,并且我的最爱之一似乎是dust.js在同步回调中​​使用dust.js(异步)

我喜欢它是异步的想法,即我只为某个模板渲染渲染,当它准备好时,回调会将渲染的HTML插入到DOM中。

但我不确定如何解决同步回调中​​的异步渲染问题。例如 - 我经常使用DataTables插件,它提供了一些回调,允许我在实际插入之前修改DOM节点。

为了说明问题 - 让我们假设我有下面的代码片段(从DataTables website采取和修改):

$(document).ready(function() { 
    $('#example').dataTable({ 
    "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { 
     // modify the node before it's actually inserted into the document 
     $('td:eq(4)', nRow).html('<b>' + aData[4] + '</b>'); 
    } 
    }); 
}); 

现在 - 我想摆脱'<b>' + aData[4] + '</b>',并使用一些方面,而不是渲染模板(这是一个微不足道的例子,但显示了这个问题)。

如果我理解正确的话,我不能强迫dust.js同步呈现模板,因此它可能会发生未处理的节点将被插入到文档(用户将看到未加工的),后来当dust.js实际呈现该节点将被修改的模板。

从用户的角度来看,这显然不太好。

所以这是真的(dust.js不能被迫同步),如果是的话 - 如何应对呢?我是否应该使用同步引擎,如handlebarsmustache或者我在这里看不到明显的东西?

任何帮助或见解或建议将非常受欢迎。 谢谢! :)

编辑:

这个问题不是关于如何使一个模板,但如何确保它的fnRowCallback完成之前呈现。感谢@robertklep指出你的(删除)答案。

+0

删除,因为我意识到这不是一个回答您的问题:)无论如何,你不能强迫一个异步函数要准备好fnRowCallback'完成之前',所以如果这是一个真正的问题我不知道认为Dust.js是可用的。 – robertklep 2013-04-09 18:40:43

+0

是的,我注意到 - 谢谢你试图帮助,但! :)是的 - 这就是我所担心的 - 有些图书馆希望你从同步回调中​​返回(或者做某件事),而dust.js可能不会和他们一起玩:( – kgr 2013-04-09 19:11:22

回答

-2

编辑:为了说清楚,我认为这是一个不好的做法,你通常不应该这样做。我将下面的代码仅仅作为一个例子,说明如果技术或体系结构的限制迫使你这样做,那么COULD可以做这样的事情。但是,它只能作为最后的手段使用。

我已经做了一些讨厌的事情,涉及到使用布尔和循环来“捕获”异步调用,但我不会真的推荐它作为一个特别好的做法。尽管如此,如果你想要的话,就在这里。

// name is the template identifier, template is the template contents, 
// data is the "context" of the template (its model), callback (optional) 
// is the method you want the rendered template passed to, cbContext 
// (optional) is the context in which you want the callback to execute 
asyncToSync = function (name, template, data, callback, cbContext) { 
    // Tracks if it's time to return or not 
    var finished = false; 
    // Gives us an exit condition in case of a problem 
    var maxLoops = 10000; 
    // Create a variable to store the return value 
    var outVal = 'Template rendering did not finish'; 
    // And the callback function to pass to dust 
    var dustCb = function (err, html) { 
     finished = true; 
     outVal = html; 
    } 
    var i = 0; 
    // We create this as a function so that increment goes to the bottom 
    // of the execution queue, giving dustCb a chance to execute before 
    // the counter hits max. 
    var incrementCounter = function() { 
     i += 1; 
    }; 
    // Compile, load, and render the template 
    var compiled = dust.compile(template, name); 
    dust.loadSource(compiled); 
    // Pass our callBack so the flag gets tripped and outVal gets set 
    dust.render(name, data, dustCb); 
    // count up to maxLoops 
    while(!finished && (i < maxLoops)) { 
     incrementCounter(); 
    } 
    // If a callback is defined, use it 
    if (callback) { 
     // If a callback context is defined, use it 
     return (cbContext) ? callback.call(cbContext, outVal) : callback(outVal); 
    } 
    // otherwise just return the rendered HTML 
    return outVal; 
} 
+0

)如果这是不好的做法,不要让它成为一个潜在的答案。 – Trevor 2013-04-18 00:24:59

+1

更何况这将阻止一切。 – Trevor 2013-04-18 00:28:07

+0

我不是在讽刺或试图粗鲁。如果你需要诉诸“讨厌的东西”,那么你应该重新考虑你的设计。顺便说一下,这对促进不良做法也没有帮助。获得一些人的技能,男人。 – Trevor 2013-07-05 19:23:39