Mustach和jquery出错?
我在调用外部json/模板的jquery小部件中使用Mustach。我的Jquery看起来不错。但是他给了我一个错误。 脚本如下: TypeError:this.tail.search不是一个函数Mustach和jquery出错?,jquery,widget,mustache,Jquery,Widget,Mustache,我在调用外部json/模板的jquery小部件中使用Mustach。我的Jquery看起来不错。但是他给了我一个错误。 脚本如下: TypeError:this.tail.search不是一个函数 [在此错误上中断] 我在Firefox中遇到了这个错误,但奇怪的是,不是Chrome。原来这是由于运行mustache.render()时模板数据为False 事实证明,该项目已经有了由其他人编写的代码来满足这一需求,我在经历了很多痛苦之后发现,允许其他组件在加载模板时进行检查: htmltempla
[在此错误上中断]
我在Firefox中遇到了这个错误,但奇怪的是,不是Chrome。原来这是由于运行
mustache.render()
时模板数据为False
事实证明,该项目已经有了由其他人编写的代码来满足这一需求,我在经历了很多痛苦之后发现,允许其他组件在加载模板时进行检查:
htmltemplate.js:
function HTMLTemplate(location)
{
this.template = null;
var self = this;
$.get(staticURL+location, function(data)
{
self.template = data;
});
}
HTMLTemplate.prototype.getTemplate = function()
{
if(this.template)
{
return this.template
}
else
{
return false;
}
}
mediabootstrap.js:
var MediaBootstrap = {};
MediaBootstrap.templates = {};
MediaBootstrap.init = function()
{
this.templates.mytemplate = new HTMLTemplate('js/templates/mytemplate.html');
}
MediaBootstrap.templatesLoaded = function()
{
var loaded = true;
for(key in this.templates)
{
if(!this.templates[key].getTemplate())
{
loaded = false;
break;
}
}
return loaded;
}
myobject.js:
MyObject.buildOptions = function(data)
{
if(!MediaBootstrap.templatesLoaded())
{
setTimeout(function(){
MyObject.buildOptions(data);
}, 100);
return;
}
var tpl = MediaBootstrap.templates.mytemplate.getTemplate();
var html = Mustache.render(tpl, data);
this.element.find('div.container').html(html);
}
我在Firefox中遇到了这个错误,但奇怪的是,不是Chrome。原来这是由于运行
mustache.render()
时模板数据为False
事实证明,该项目已经有了由其他人编写的代码来满足这一需求,我在经历了很多痛苦之后发现,允许其他组件在加载模板时进行检查:
htmltemplate.js:
function HTMLTemplate(location)
{
this.template = null;
var self = this;
$.get(staticURL+location, function(data)
{
self.template = data;
});
}
HTMLTemplate.prototype.getTemplate = function()
{
if(this.template)
{
return this.template
}
else
{
return false;
}
}
mediabootstrap.js:
var MediaBootstrap = {};
MediaBootstrap.templates = {};
MediaBootstrap.init = function()
{
this.templates.mytemplate = new HTMLTemplate('js/templates/mytemplate.html');
}
MediaBootstrap.templatesLoaded = function()
{
var loaded = true;
for(key in this.templates)
{
if(!this.templates[key].getTemplate())
{
loaded = false;
break;
}
}
return loaded;
}
myobject.js:
MyObject.buildOptions = function(data)
{
if(!MediaBootstrap.templatesLoaded())
{
setTimeout(function(){
MyObject.buildOptions(data);
}, 100);
return;
}
var tpl = MediaBootstrap.templates.mytemplate.getTemplate();
var html = Mustache.render(tpl, data);
this.element.find('div.container').html(html);
}
只是jQuery1.8.x也有同样的问题。我通过将html模板呈现为字符串而不是html对象解决了这个问题。使用数据类型:“text”,您将获得一个包含模板html内容的干净字符串。渲染引擎可以很好地使用它。
测试:firefox和chrome
$.ajax({
type: 'GET',
url: './htmlTemplates/conferenceMembers.htpl',
dataType: 'text',
success: function (data) {
$(divId).append(Mustache.render(data, htmlRenderValues));
}
});
注意:jQuery2.0.1解决了这个问题。html对象上的渲染工作正常。jQuery1.8.x也有同样的问题。我通过将html模板呈现为字符串而不是html对象解决了这个问题。使用数据类型:“text”,您将获得一个包含模板html内容的干净字符串。渲染引擎可以很好地使用它。
测试:firefox和chrome
$.ajax({
type: 'GET',
url: './htmlTemplates/conferenceMembers.htpl',
dataType: 'text',
success: function (data) {
$(divId).append(Mustache.render(data, htmlRenderValues));
}
});
注意:jQuery2.0.1解决了这个问题。html对象上的渲染工作正常。当我不小心切换了模板字符串和对象参数时,我遇到了这个问题。
它应该是:
Mustache.render({{name}}模板的工作原理是这样的,{name:“Mustache”})代码>当我不小心切换了模板字符串和对象参数时,我遇到了这个问题。
它应该是:Mustache.render({{name}}模板的工作原理是这样的,{name:“Mustache”})代码>使用哪种浏览器?使用哪种浏览器?我真的不明白为什么Chrome在没有这个的情况下工作,也许它能更快地加载模板?我真的不明白为什么Chrome在没有这个的情况下工作,也许它能更快地加载模板?为了简单起见,我几乎完全使用$.get
而不是$.ajax
(正如最初的提问者在他的问题中所做的那样)。因此,我发现添加'text'
参数更方便,如$.get('./htmlTemplates/conferenceMembers.htpl',函数(数据){…},'text')
为了简单起见,我几乎只使用$.get
而不是$.ajax
(正如最初的提问者在他的问题中所做的那样)。因此,我发现添加'text'
参数更方便,如$.get('./htmlTemplates/conferenceMembers.htpl',函数(数据){…},'text')代码>