将Knockout与jQuery混合使用 [英] Mixing Knockout with jQuery
问题描述
我正在使用knockout.js创建一个评论系统,我在使用我们现有的jQuery函数进行模板化时遇到了一些问题。
I'm creating a commenting system with knockout.js and I'm having some issues with getting the templating working with our existing jQuery functions.
一个例子是日期评论已创建。我写了一个jQuery函数,导致数据从 5-5-2012
转到 2天前
。例如:
One example is with the dates comments are created. I wrote a jQuery function that causes the data to turn from 5-5-2012
to 2 Days ago
. For example:
<ul data-bind="foreach: Comments">
<li data-bind="attr: { id: Id }" class="Comment">
<div data-bind="text: DateCreated" class="prettyDate"></div>
...
</li>
</ul>
<script type="text/javascript">
$(function(){
$(".prettyDate").prettify();
});
</script>
使用此代码,当我动态添加新评论时,日期保持在 5-5-2012
格式。还有其他一些自定义jQuery函数可以处理重复数据,这些函数现在由knockout动态创建(通常通过基于类选择)。
With this code, when I add a new comment dynamically, the date stays in the 5-5-2012
format. There are several other custom jQuery functions that act on repeating data that is now dynamically created by knockout (usually by selecting based on classes).
如何应用这些自定义jQuery由knockout.js生成的动态数据的函数?
How can I apply these custom jQuery functions on dynamic data generated by knockout.js?
推荐答案
一个选项可能是使用自定义绑定处理程序通过它发送绑定元素你的jQuery插件,例如:
One option might be to use a custom binding handler which sends bound elements through your jQuery plugin, e.g.:
http:/ /jsfiddle.net/mikebridge/Q9r86/4/
另一种可能性是在视图模型中添加计算的observable。
Another possibility might be to add a computed observable in your view model.
这篇关于将Knockout与jQuery混合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!