【发布时间】:2012-12-04 16:27:26
【问题描述】:
最新编辑:我在问题末尾添加了我的解决方案,基于 chrisvillanueva 的回答
我有一个创建按钮的辅助方法,当单击此按钮时会出现一个对话框(jQuery UI 对话框),该按钮在页面上出现多次。
辅助方法包含我只想呈现一次的部分,例如 javascript 代码和单击按钮时出现的对话框的 HTML。 (基本上唯一应该多次渲染按钮本身的 HTML 的东西)
为了确保仅在我使用 Cassette (http://getcassette.net/) 时加载 javascript,但我找不到对话框 HTML 的好解决方案,我看到 Cassette 有 HTML 模板,但它是相当复杂,因为 HTML 是在脚本标签中呈现的。
目前我的解决方法是使用 Cassette 的 HTML 模板,并在以 开头和结尾的模板中,这样它消除了包装块并且 HTML 由浏览器呈现 - 我知道,这很糟糕......:/ 这是我目前与 Cassette 的 HTML 模板一起使用的 HTML
</script> <!-- This closes the starting <script> that is generated by Cassette -->
<div class="recommendDialog" title="Recommend This Goal To A Friend">
</div>
<script> <!-- This is for the closing </script> that is generated by Cassette -->
希望有任何想法,最好是带有 Cassette 或内置在 MVC3 中的东西(我不喜欢添加更多库)
编辑:也许还有一种简单的方法可以将 jQuery UI 的对话框与脚本块内的 html 一起使用(我尝试使用它并进行搜索,但除了将内容“推送”到另一个 html 元素中之外找不到任何东西)
示例代码:
@helper BarControl(WebViewPage<MyModel> page)
{
// Currently using Cassette for javascripts
Cassette.Views.Bundles.Reference("barcontrol", "body");
// I tried using Cassette HTML Templates but the rendered HTML is in a <script> tag
// which I can't use with jQuery UI's dialog widget
// Cassette.Views.Bundles.Reference("barcontroltemplates", "body");
// This HTML is for the dialog, should only be rendered once in the final HTML
<div class="dialog" title="I'm a dialog">
<span>I'm a dialog</span>
</div>
// This is the HTML for the button which should be rendered every time this helper is called
<div class="area">
<div class="right">
<ul>
<li id="button"><a href="#">I'm a button</a></li>
</ul>
</div>
</div>
}
javascript 只是在带有$(".dialog").dialog() 的按钮上设置一个点击事件
我最终做了什么:
基于克里斯维拉纽瓦的回答。
我使用 Cassette 的 HtmlTemplates 来呈现这个 HTML:
<script id="dialogHtmlTemplate" type="text/html">
<div class="dialog" title="dialog">
<span>Content of dialog</span>
</div>
</script>
为了将它与 jQuery UI 的对话框 (http://api.jqueryui.com/dialog/) 一起使用,我使用以下代码:$($("#dialogHtmlTemplate").html()).dialog()
这会通过浏览器呈现 html 并在其周围创建对话框,它无需添加更多库(如 mustache.js 或 jQuery 模板)即可工作,对于上述情况(没有模板只是重复)是不需要的。
就性能而言,我认为这不是最好的解决方案(它每次都会复制内部 html),但现在它已经足够好了......
【问题讨论】:
-
添加了一些代码,还添加了我目前在 Cassette 中使用的内容,如果您需要其他任何内容,请告诉我,我会添加,谢谢!
标签: asp.net-mvc jquery-ui razor html-helper cassette