ReferenceError:手把没有定义 [英] ReferenceError: Handlebars is not defined
本文介绍了ReferenceError:手把没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Handlebars.js完全陌生,几乎是JavaScript本身的新手。我在控制台上看到这个错误ReferenceError:Handlebars is not defined。我已经试着把这些引用放在x-handlebars-templates中,但它没有帮助。
index.tmpl.php:
< html> < meta content =text / html; charset = utf-8http-equiv =Content-Type> < meta content =utf-8http-equiv =encoding> < link rel =stylesheettype =text / csshref =style.css>< title> PHP和jquery< / title>< / head>< body> < div class =container> < h1>按标题搜索电影:< / h1>< form id =film-selectionaction =index.phpmethod =post> < select name =qid =q> <?php $ alphabet = str_split('abcdefghijklmnopqrstuvwxyz'); foreach($字母为$字母){echo< option value ='$ letter'> $ letter< / option>; ?}> < /选择> < button type =submit> Go!< / button>< / form> <?php if(isset($ film)):?> < ul class =film-list> <?php foreach($ film as $ a){echo< data-film_id ='{$ a-> film_id}'>< a href ='film.php?film_id = {$ a- > film_id}'> {$ a-> title}< / a>< / li>; ?}> < / UL> <?php endif; ?>< script id =film_list_templatetype =text / x-handlebares-templates> {{#each this}}< li data-film_id ={{film_id}}> < a href =film.php?film_id = {{film_id}}> {{fulName this}}< / a> < /锂> {{/每}}< /脚本> < script type =text / javascriptsrc =js / jquery.js>< / script> < script type =text / javascriptsrc =js / handlebars-v2.0.0.js>< / script> < script type =text / javascriptsrc =js / script.js>< / script> < / div>< / html>
p> script.js:
var Film = {init:function(config){this.config = config; this.setupTemplates(); this.bindEvents(); },bindEvents:function(){this.config.letterSection.on('change',this.fetchFilm); },setupTemplates:function(){//错误在下面的这一行中。 this.config.filmListTemplate = Handlebars.compile(this.config.filmListTemplate); //这里是错误Handlebars.registerHelper('fulName',function(film){return film.title;}); },fetchFilm:function(){alert('Test2'); var self =电影; $ .ajax({url:'index.php',type:'POST',data:self.config.form.serialize(),dataType:'json',success:function(results){//console.log结果[0] .title); self.config.filmList.append(self.config.filmListTemplate(results));}}); }}; Film.init({letterSection:$('#q'),form:$('#film-selection'),filmListTemplate:$('#film_list_template')。html(),filmList:$('ul .film-list')});
解决方案
我发现我的js目录中没有handlebars-v2.0.0.js文件。
I am completely new to Handlebars.js and almost new to JavaScript itself. I'm getting this error on the console "ReferenceError: Handlebars is not defined". I have already tried to put alle the references befor the "x-handlebars-templates" but it did not help.
index.tmpl.php:
<html>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<link rel="stylesheet" type="text/css" href="style.css">
<title>PHP and jquery</title>
</head>
<body>
<div class="container">
<h1>Search films by title:</h1>
<form id="film-selection" action="index.php" method="post">
<select name="q" id="q">
<?php
$alphabet = str_split('abcdefghijklmnopqrstuvwxyz');
foreach ($alphabet as $letter) {
echo "<option value='$letter'>$letter</option>";
}
?>
</select>
<button type="submit">Go!</button>
</form>
<?php if(isset($film)): ?>
<ul class="film-list">
<?php
foreach ($film as $a) {
echo " <li data-film_id='{$a->film_id}'>
<a href='film.php?film_id={$a->film_id}'>{$a->title} </a>
</li>";
}
?>
</ul>
<?php endif; ?>
<script id="film_list_template" type="text/x-handlebares-templates">
{{#each this}}
<li data-film_id="{{film_id}}">
<a href="film.php?film_id={{film_id}}">{{fulName this}}</a>
</li>
{{/each}}
</script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/handlebars-v2.0.0.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</div>
</body>
</html>
script.js:
var Film = {
init: function(config){
this.config = config;
this.setupTemplates();
this.bindEvents();
},
bindEvents: function(){
this.config.letterSection.on('change', this.fetchFilm);
},
setupTemplates: function(){
//The error is in this line below.
this.config.filmListTemplate = Handlebars.compile(this.config.filmListTemplate); //here is the error
Handlebars.registerHelper('fulName', function(film){
return film.title;
});
},
fetchFilm: function(){
alert('Test2');
var self = Film;
$.ajax({
url: 'index.php',
type: 'POST',
data: self.config.form.serialize(),
dataType: 'json',
success: function(results){
//console.log(results[0].title);
self.config.filmList.append(self.config.filmListTemplate(results));
}
});
}
};
Film.init({
letterSection: $('#q'),
form: $('#film-selection'),
filmListTemplate: $('#film_list_template').html(),
filmList: $('ul.film-list')
});
解决方案
I found out that i had not the file"handlebars-v2.0.0.js" in my js directory.
这篇关于ReferenceError:手把没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文