因此,我正在使用RequireJs,Mustache和Backbone.js制作测试应用程序。用Mustache模板渲染模型集合,我取得了一些成功。但我的小胡子模板有一个按钮,当我尝试绑定视图中按钮上的点击事件时,按钮点击不会调用回调函数。我真的陷入了困境,有人能告诉我我哪里做得不对吗?与Backbone.js一起使用胡子时事件不起作用
这里是我的代码:
ItemView.js:
define(['jquery', 'backbone', 'underscore', 'mustache', '../../atm/model/item'], function ($, Backbone, _, Mustache, Item) {
var ItemView = Backbone.View.extend({
initialize: function() {
},
tagName: 'li',
events: {
'click .button': 'showPriceChange'
},
render: function() {
var template = $('#template-atm').html();
var itemObj = this.model.toJSON();
itemObj['cid'] = this.model.cid;
var rendering = Mustache.to_html(template, itemObj);
this.el = rendering;
return this;
},
showPriceChange: function(event) {
alert('Changing...');
$('#' + elemId).empty();
$('#' + elemId).append(document.createTextNode('Changed'));
},
});
return ItemView;
});
atm.html:
<!DOCTYPE html>
<html>
<head>
<title>Elevator</title>
<script data-main="scripts/main" src="scripts/require-jquery.js"></script>
<style type="text/css">
</style>
</head>
<body>
<h1>Vending Machine</h1>
<div id="atm-items">
</div>
<script id="template-atm" type="html/template">
<li>
<p>Item: {{name}}</p>
<label for="price-{{cid}}">Price:</label>
<input id="price-{{cid}}" type="text" value="{{price}}"/>
<button class="button">Change</button>
<p id="status-{{name}}-{{cid}}">- -</p>
</li>
</script>
</body>
</html>
我认为它做了一个帖子/提交时,当你点击按钮。这是按钮的默认设置。尝试添加'type =“按钮”'并告诉我会发生什么。 – Deeptechtons
没有去:-(我改变了按钮元素为,但仍然没有事件 – DaHoopster