学习有关backbone.js View 的非常基本的教程。
预期的行为是在单击 #sayhello 按钮时调用渲染函数。 Render只是使用jQuery的html方法将“hello Bud Abbot”放入el中。
但是当我单击#sayhello 按钮时,没有任何反应。没有错误或任何东西。我在 firebug 中设置了一个断点,然后观察它跳过渲染函数。
这是js:
App = (function($){
jQueryView = Backbone.View.extend({
initialize: function(){
this.el = $(this.el);
}
});
HelloWorldView = jQueryView.extend({
el: $('#helloworld'),
events:{ 'click #sayhello': 'render'
},
initialize: function(params){
jQueryView.prototype.initialize.call(this);
this.name = params.name;
},
render: function(){
console.log("rendering");
this.el.html("hello " + this.name);
}
});
var self = {};
self.start = function(){
new HelloWorldView({name: 'Bud Abbot'});
};
return self;
});
这是 html:
<div id="helloworld"></div>
<button id="sayhello">Say Hello</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
当我改变这个时,看起来很奇怪:
new HelloWorldView({name: 'Bud Abbot'});
对此:
new HelloWorldView({name: 'Bud Abbot'}).render();
调用了渲染方法,但是当我尝试使用事件来执行此操作时 - 没有骰子。非常感谢您帮助理解我做错了什么。
最佳答案
这是因为#sayhello 不属于您的 View 。尝试将#sayhello 放入#helloworld 中:
<div id="helloworld">
<button id="sayhello">Say Hello</button>
</div>
关于jquery - backbone.js 点击事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7670383/