简体   繁体   English

通过Javascript动态渲染AlloyUI模态正文HTML

[英]Dynamically Rendering AlloyUI Modal body HTML via Javascript

I have a form created via javascript that will be set as the content of a modal widget provided by Alloy UI . 我有一个通过javascript创建的表单,它将被设置为Alloy UI提供的模态小部件的内容。 However I don't feel I am approaching this the right way. 但是我觉得我没有以正确的方式接近这一点。 This question applies for javascript-created HTML in general. 这个问题一般适用于javascript创建的HTML。

I realize I should avoid using global variables for the sake of namespace and collision, so maybe someone can help me out. 我意识到我应该避免使用全局变量来进行命名空间和冲突,所以也许有人可以帮助我。 I declare the global var myForm and initialize it with some HTML elements to create a form: 我声明了全局var myForm并使用一些HTML元素对其进行初始化以创建表单:

var myForm = '<form id="my-form" method="get"> 
<div> 
  <select> 
    <option value="option-1">Option 1</option> 
    <option value="option-2">Option 2</option> 
    <option value="option-3">Option 3</option> 
  </select> 
</div> 
</form>'

And a modal widget that sets myForm as the body content: 还有一个模态小部件,它将myForm设置为正文内容:

var modal = new Y.Modal(
    {
        bodyContent: myForm,
        centered: true,
        modal: true,
        resizable: false,
        draggable: false,
        width: 350
    }).render();

This seems like a not-so-great way of using Javascript to render HTML, especially for my project since I will have many html elements created via javascript ( <img> 's, <ul> 's etc), so I don't want to get into a bad coding habit. 这似乎是使用Javascript渲染HTML的一种不那么好的方式,特别是对于我的项目,因为我将通过javascript( <img><ul>等)创建许多html元素,所以我不喜欢我想陷入糟糕的编码习惯。 Is there a more standard or acceptable way to dynamically render HTML (not speaking from a templating perspective, ie, Thymeleaf, etc)? 是否有更标准或可接受的方式来动态呈现HTML(不是从模板的角度来讲,即Thymeleaf等)? Thanks all 谢谢大家

An alternative solution is to specify the modal and it's contents in your html, and then let AlloyUI take over and turn it into a modal dialog. 另一种解决方案是在html中指定模态及其内容,然后让AlloyUI接管并将其转换为模态对话框。 You can do this by specifying the boundingBox and contentBox of the Modal as the div s which contain the html you want inside the modal: 您可以通过将Modal的boundingBoxcontentBox指定为包含模式中所需html的div来完成此操作:

 YUI().use('aui-modal', function(Y) { new Y.Modal({ boundingBox: '#bb', contentBox: '#cb', centered: true, modal: true, resizable: false, draggable: false, width: 350 }).render(); }); 
 <link href="http://cdn.alloyui.com/2.0.0/aui-css/css/bootstrap.min.css" rel="stylesheet" /> <script src="http://cdn.alloyui.com/2.0.0/aui/aui-min.js"></script> <div id="bb"> <div id="cb"> <form id="my-form" method="get"> <div> <select> <option value="option-1">Option 1</option> <option value="option-2">Option 2</option> <option value="option-3">Option 3</option> </select> </div> </form> </div> </div> 

我会在脚本标记内声明模板,如下所述: <script type =“text / template”> ... </ script>的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM