骨干视图单击hello world
Backbone View click hello world
我正在尝试用Backbone做一个非常简单的hello world。阅读文档和示例后,我认为这应该工作,但不做任何事情,当我点击:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
<script type="text/javascript">
var AppView = Backbone.View.extend({
events: {
'click #new': 'createNew'
},
//CREATE
createNew: function(){
alert('yea');
}
});
var app_view = new AppView();
</script>
</head>
<body>
<a href="#new" id="new">new</a>
</body>
</html>
我错过了什么吗?会出什么问题呢?
这里有两个问题:
- 主干视图绑定到一个特定的DOM元素。你必须把你的视图附加到DOM上,然后它才能响应事件。
- 当脚本执行时,DOM可能还没有准备好,你必须用
$(document).ready
包装视图创建代码。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
<script type="text/javascript">
var AppView = Backbone.View.extend({
events: {
// the element IS the link, you don't have to specify its id there
'click': 'createNew'
},
//CREATE
createNew: function(){
alert('yea');
}
});
$(document).ready(function() {
// attach the view to the existing a element
var app_view = new AppView({
el: '#new'
});
});
</script>
</head>
<body>
<a href="#new" id="new">new</a>
</body>
</html>
相关文章:
- 谷歌图表-“;快速入门Hello World;不在IE8中渲染
- 如何下载杜兰达尔示例项目(Hello World)的源代码
- 无法让我的“hello world”YUI asyncRequest javascript 运行,并且由于某种原因,我无
- Knockout.js-简单"Hello World"失败
- AngularJS + Coffeescript - 'Hello World' 指令不起作用
- AWS 上的 Node JS “Hello world” 服务器
- 在javascript中将hello world返回到olleh dlrow
- JSNI Hello World is not working
- redux-react simple hello world
- Jointjs Hello World 空白,控制台中没有错误
- 我的“Hello World”Angular JS无法正常工作
- 节点.js服务器上的“Hello World”
- 为什么获胜't This Vuejs“;Hello World"示例在我的电脑上渲染
- Hello World示例的Node.js错误
- doh Hello World,带有混合'的内容窗格
- Hello World on PhoneGap Build
- JointJS:Hello World的例子不起作用
- 骨干视图单击hello world
- 如何在linux专用服务器上运行简单的node.js Hello World程序
- angularJS中的Hello World不起作用