【发布时间】:2017-12-10 17:06:23
【问题描述】:
我想设置一个部分来呈现一个通用的、可定制的模式。 {{> myModal }}.
我想通过发送一个对象从调用视图自定义模态。
我已经尝试过这里推荐的方法:Passing an array of objects to a partial - handlebars.js
这是我在视图中调用部分的代码:
{{# getJsonContext '
{
"id": "deleteModal",
"title": "Are you sure?",
"formId": "delete-form",
"body": "Press Yes to delete this record. Press No to cancel."
}
'}}
{{> myModal this }}
{{/ getJsonContext }}
这是我的助手:
getJsonContext: function(data, options) {
console.log(data); <-- The result is correct.
let jsonReturn = JSON.parse(data);
console.log(jsonReturn.title); <-- The result is correct.
return jsonReturn;
},
这是我的部分:
<div class="modal fade" id="{{ id }}">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header bg-light">
<h4 class="modal-title">{{ title }}</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Form -->
<form id="{{ formId }}" method="post">
<!-- Modal body -->
<div class="modal-body">
{{ body }}
</div>
<!-- Modal footer -->
<div class="modal-footer rounded-bottom bg-light">
</div>
</form>
</div>
</div>
</div>
我希望部分视图能够呈现自定义模式。
部分似乎根本没有渲染。当我在浏览器中“查看页面源代码”时,在所有部分<html> 的位置,它显示的只是[object Object]。
如果我将{{> myModal this}} 放在helper 之外,“查看页面源代码”会显示所有模态的<html>, 但自然地,id、title、formId 和 body 都是空白的(null )。
【问题讨论】:
-
再次查看您引用的答案中的
getJsonContext助手。该示例中的return options.fn(JSON.parse(data));与您的 return 语句之间存在很大差异。
标签: handlebars.js partial