gpt4 book ai didi

javascript - backbone.js 在脚本中找不到错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:11:30 25 4
gpt4 key购买 nike

我是 javascript 和 backbone.js 的新手我想创建一个简单的网页,其中包含 10 个正方形,其形式将采用正方形 ID 和颜色。

所以每个方 block 都必须在 CSS 中有自己的样式。

我试着用 10 个模板制作它。但是脚本根本不起作用。

这是我的代码:

alert("script entry");

$(function () {


blocks = [
{number: "1", state: "block1" },
{number: "2", state: "block2" },
{number: "3", state: "block3" },
{number: "4", state: "block4" },
{number: "5", state: "block5" },
{number: "6", state: "block6" },
{number: "7", state: "block7" },
{number: "8", state: "block8" },
{number: "9", state: "block9" },
{number: "10", state: "block10" },
];

var BlockModel = Backbone.Model.extend({
defaults:{
"state": "block1",
"number": "1"
}
});
var BlockCollection = Backbone.Collection.extend({
model: BlockModel,
});
var blockNumbers = new BlockCollection([
model:BlockModel
]);

var BlockView = Backbone.View.extend({
tagName: "blockTag",
className: "blockClass",
templates: {
"block1": _.template($('#block1').html()),
"block2": _.template($('#block2').html()),
"block3": _.template($('#block3').html()),
"block4": _.template($('#block4').html()),
"block5": _.template($('#block5').html()),
"block6": _.template($('#block6').html()),
"block7": _.template($('#block7').html()),
"block8": _.template($('#block8').html()),
"block9": _.template($('#block9').html()),
"block10": _.template($('#block10').html())
},

render: function () {
var state= this.model.get("state");
var tmpl = this.templates(state);
$(this.el).html(tmpl(this.model.toJSON()));
return this;
}
});



var appView = Backbone.View.extend({
el: $("#block"), //большой контейнер

initialize: function(){
this.collection = new blockNumbers(blocks);
this.render();
},

render: function(){

_.each(this.collection.models, function () {
that.renderBlock(this.model);
}, this);
},

renderBlock: function (inputModel) {
var blockView = new BlockView({
{model: inputModel}
});
this.$el.append(blockView.render().el);
}


});

var app = new appView();


});

我的错误在哪里?我的 index.html :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TEST</title>
<link rel="stylesheet" type="text/css" href="style.css">



</head>
<body>

<div id="block">



<script type="text/template" id="block1">
<div class="block1"><%=number%></div>
<div class="buttonplace">
<input type="button" value="check" />
</div>
</script>

<script type="text/template" id="block2">
<div class="block2"><%=number%></div>
</script>

<script type="text/template" id="block3">
<div class="block3">3</div>
</script>

<script type="text/template" id="block4">
<div class="block4">4</div>
</script>

<script type="text/template" id="block5">
<div class="block5">5</div>
</script>

<script type="text/template" id="block6">
<div class="block6">6</div>
</script>

<script type="text/template" id="block7">
<div class="block7">7</div>
</script>

<script type="text/template" id="block8">
<div class="block8">8</div>
</script>

<script type="text/template" id="block9">
<div class="block9">9</div>
</script>

<script type="text/template" id="block10">
<div class="block10">10</div>

</script>


</div>


<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="underscore.js"></script>
<script type="text/javascript" src="backbone.js"></script>

<script type="text/javascript" src="script.js"></script>



</body>
</html>

最佳答案

您的代码有几个问题,主要是您在某些情况下使用圆括号而不是方括号 [ 和花括号 {。您还有一些额外的尾随逗号。此外,在您的 HTML 中,block div 的结束标记应该位于所有脚本模板之前。

例如

var blockNumbers = new BlockCollection([  
model:BlockModel
]);

应该是

var blockNumbers = new BlockCollection({  
model:BlockModel
});

var tmpl = this.templates(state); 

应该是

 var tmpl = this.templates[state]; 

此外,在您的 appView 中,您正在尝试实例化您的实例的新实例

this.collection = new blockNumbers(blocks);

虽然你可能打算这样做

this.collection = new BlockNumbers(blocks);

这是一个有效的 jsBin

除了你的模板有很多重复之外,你真的可以将它们全部合并到一个或两个模板中。

例如,您可以只为带按钮的 block 使用一个模板,为不带按钮的 block 使用一个模板,并相应地更新 block 状态

  <script type="text/template" id="blockWithButton">
<div class="block<%=number%>"><%=number%></div>
<div class="buttonplace">
<input type="button" value="check" />
</div>
</script>

<script type="text/template" id="blockWithoutButton">
<div class="block<%=number%>"><%=number%></div>
</script>

另一个jsbin

关于javascript - backbone.js 在脚本中找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com