我希望能够根据我从搜索中检索到的值来更改模板。我想过改变这样的变量内容:
search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
hitsPerPage: 12,
templates: {
empty: noResultsTemplate,
item: hitTemplate
},
transformData: function (hit) {
if (hit.myVariable == true) {
this.templates.item = otherTemplateVariable;
}
return hit;
}
})
);
但我收到错误:'Unable to get property 'templates' of undefined or null reference'
答案 0 :(得分:0)
问题似乎是您引用this
并指向没有templates属性的函数transformData
,尝试提取您的对象以访问它:
var data = {
container: '#hits',
hitsPerPage: 12,
templates: {
empty: noResultsTemplate,
item: hitTemplate
},
transformData: function (hit) {
if (hit.myVariable == true) {
//access the templates attribute
data.templates.item = otherTemplateVariable;
}
return hit;
}
};
search.addWidget(
instantsearch.widgets.hits(data)
);