Handlebars 方法调用没有 return 值
Handlebars method call doesn't return a value
我使用车把。我注册了一个调用函数的助手
Handlebars.registerHelper("getContactCategoryById", getContactCategoryById);
函数
function getContactCategoryById(categoryId) {
var category = sessionStorage.getItem("category");
$.each(jQuery.parseJSON(category), function () {
if (this.contactCategoryId == categoryId) {
return this.name;
}
});
}
在我的模板中,我的函数被调用,当我调试时,getContactCategoryById return 一个值,但它从未显示在 table 中。
当我看到 html 代码时,我可以看到 contact-category-id 具有 data-contact-category-id 标签的值。
<script id="lodger-contact-available-result-template" type="text/x-handlebars-template">
<table id="lodgerContactAvailableTableResult" style="min-height:200" data-show-header="true" class="table table-striped" data-toggle="table" data-height="330">
<thead>
<tr>
<th>Category</th>
</tr>
</thead>
<tbody>
{{#each this}}
<tr>
<td data-contact-category-id={{contactCategoryId}}>{{getContactCategoryById contactCategoryId}}</td>
</tr>
{{/each}}
</tbody>
</table
</script>
编辑:解决方案,需要做一个return来存在每个并获取值
function getContactCategoryById(categoryId) {
var category = sessionStorage.getItem("category");
var toReturn;
$.each(jQuery.parseJSON(category), function () {
console.log(this.contactCategoryId, categoryId);
if (this.contactCategoryId == categoryId) {
toReturn = this.name;
return false;
}
});
return toReturn;
}
问题是您 return 来自本身就是一个函数的 jQuery.each
,因此您的 return 值被 jQuery.each
和您的助手吞没了函数 getContactCategoryById
最终没有 returning 任何东西(或者准确地说总是 returning undefined
)。
您需要做的是在找到您需要的内容后停止循环,然后 return 循环完成后您找到的内容:
function getContactCategoryById(categoryId) {
var category = sessionStorage.getItem("category");
var foundName = '';
$.each(jQuery.parseJSON(category), function () {
if (this.contactCategoryId == categoryId) {
foundName = this.name;
return false; // returning false in jQuery.each stops the iteration
}
});
return foundName; // return the name you found
}
我使用车把。我注册了一个调用函数的助手
Handlebars.registerHelper("getContactCategoryById", getContactCategoryById);
函数
function getContactCategoryById(categoryId) {
var category = sessionStorage.getItem("category");
$.each(jQuery.parseJSON(category), function () {
if (this.contactCategoryId == categoryId) {
return this.name;
}
});
}
在我的模板中,我的函数被调用,当我调试时,getContactCategoryById return 一个值,但它从未显示在 table 中。 当我看到 html 代码时,我可以看到 contact-category-id 具有 data-contact-category-id 标签的值。
<script id="lodger-contact-available-result-template" type="text/x-handlebars-template">
<table id="lodgerContactAvailableTableResult" style="min-height:200" data-show-header="true" class="table table-striped" data-toggle="table" data-height="330">
<thead>
<tr>
<th>Category</th>
</tr>
</thead>
<tbody>
{{#each this}}
<tr>
<td data-contact-category-id={{contactCategoryId}}>{{getContactCategoryById contactCategoryId}}</td>
</tr>
{{/each}}
</tbody>
</table
</script>
编辑:解决方案,需要做一个return来存在每个并获取值
function getContactCategoryById(categoryId) {
var category = sessionStorage.getItem("category");
var toReturn;
$.each(jQuery.parseJSON(category), function () {
console.log(this.contactCategoryId, categoryId);
if (this.contactCategoryId == categoryId) {
toReturn = this.name;
return false;
}
});
return toReturn;
}
问题是您 return 来自本身就是一个函数的 jQuery.each
,因此您的 return 值被 jQuery.each
和您的助手吞没了函数 getContactCategoryById
最终没有 returning 任何东西(或者准确地说总是 returning undefined
)。
您需要做的是在找到您需要的内容后停止循环,然后 return 循环完成后您找到的内容:
function getContactCategoryById(categoryId) {
var category = sessionStorage.getItem("category");
var foundName = '';
$.each(jQuery.parseJSON(category), function () {
if (this.contactCategoryId == categoryId) {
foundName = this.name;
return false; // returning false in jQuery.each stops the iteration
}
});
return foundName; // return the name you found
}