当路由与其对应的回调匹配时,Backbone.js execute 方法在路由器内部被调用。
用法:
router.execute(callback, args)
参数说明:
callback:与路由匹配时执行。
args:它指定在 execute 方法中传递的参数。
让我们举个例子。
看这个例子:
<!DOCTYPE html>
<head>
<title>Router Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
var Route1 = Backbone.View.extend({
template:'<b>This is route 1</b>',
initialize:function () {
this.execute();
},
execute:function () {
this.$el.html(this.template);
}
});
var Route2 = Backbone.View.extend({
template:'<b>This is route 2</b>',
initialize:function () {
this.execute();
},
execute:function () {
this.$el.html(this.template);
}
});
var AppRouter = Backbone.Router.extend({
routes:{
'':'homeRoute',
'route/1':'homeRoute',
'route/2':'aboutRoute',
},
homeRoute:function () {
var route1 = new Route1();
$("#content").html(route1.el);
},
aboutRoute:function () {
var route2 = new Route2();
$("#content").html(route2.el);
}
});
var appRouter = new AppRouter();
Backbone.history.start();
</script>
<body>
<div id="navigation">
<a href="#/route/1">route1</a>
<a href="#/route/2">route2</a>
</div>
<div id="content"></div>
</body>
</html>
输出:
将上述代码保存在 execute.html 文件中并在新浏览器中打开此文件。
相关用法
- Backbone.js router.route()用法及代码示例
- Backbone.js router.routes()用法及代码示例
- Backbone.js router.navigate()用法及代码示例
- Backbone.js model.url()用法及代码示例
- Backbone.js model.changed()用法及代码示例
- Backbone.js model.save()用法及代码示例
- Backbone.js model.hasChanged()用法及代码示例
- Backbone.js collection.slice()用法及代码示例
- Backbone.js view.render()用法及代码示例
- Backbone.js view.template()用法及代码示例
- Backbone.js collection.parse()用法及代码示例
- Backbone.js model.escape()用法及代码示例
- Backbone.js model.previousAttributes()用法及代码示例
- Backbone.js model.unset()用法及代码示例
- Backbone.js collection.clone()用法及代码示例
- Backbone.js collection.fetch()用法及代码示例
- Backbone.js model.idAttribute()用法及代码示例
- Backbone.js model.changedAttributes()用法及代码示例
- Backbone.js collection.url()用法及代码示例
- Backbone.js collection.length()用法及代码示例
注:本文由纯净天空筛选整理自 Backbone.js router.execute()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。