javascript - Dust.js 双向数据绑定
问题描述
我有一个非常简单的下拉菜单。
<div>
<select id="optionDropDown">
<option value="volvo">{@pre type="content" key="dropdown.option1"/}</option>
<option value="saab">{@pre type="content" key="dropdown.option2"/}</option>
<option value="mercedes">{@pre type="content" key="dropdown.option3"/}</option>
<option value="audi">{@pre type="content" key="dropdown.option4"/}</option>
</select>
</div>
每当用户更改下拉列表中的选择时,我都需要修改 UI。我将不得不添加另一个<p>
与那辆特定汽车的描述。
目前我正在使用 jQuery 执行此操作,但很想知道是否有一些内置方法可以Dust.js
通过修改context
或任何其他方式来执行此操作。
解决方案
您仍然可以使用灰尘模板实现相同的效果
<script type="text/dust" id="tpl">
<p>Chose: {type}!</p>
</script>
<script type="text/javascript">
var tpl_src = $('#tpl').html();
// Compile the template under the name 'tpl'
var tpl_compiled = dust.compile(tpl_src, 'tpl');
// Register the template with Dust
dust.loadSource(tpl_compiled);
// Whenever a choice is made...
$('select[id=types]').on('change', function() {
// Using jQuery ----------------------
// $('div#selections').append('<p>'+$(this).val()+'</p>');
// Using dustjs ----------------------
// Render the template
dust.render('tpl', { type: $(this).val() }, function(err, out) {
if(err) {
console.log(err);
return;
}
// `out` contains the rendered output.
$('div#selections').append(out);
});
});
</script>
这是完整的示例要点
推荐阅读
- python - 如何测量图表打印所需的时间
- javascript - RangeError:toDisplayDate 的时间值无效
- firebase - 如何在 Kotlin 中将数字从 firestore 动态转换为 long 或 double?
- python - 如何使用python填充终端输入字段
- ionic-framework - Ionic Cordova App 如何在离子输入字段中禁用复制和粘贴
- google-cloud-dataflow - 数据流工作者没有旋转
- java - 使用 contactGroups.members.modify 使用 People API 向新人员添加标签时出现 500 内部服务器错误
- javascript - AXIOS 调用和渲染数据
- html - 使用 CSS 的顶部波浪 div
- java - as400 acsbundle.jar IBM