本文概述
- 参数说明
句法:
router.execute(callback, args)
参数说明callback:与路由匹配时执行。
args:它指定在execute方法内传递的参数。
让我们举个例子。
【backbone.js路由执行execute】请参阅以下示例:
<
!DOCTYPE html>
<
head>
<
title>Router Example<
/title>
<
script src="http://img.readke.com/220416/0611492X2-0.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/061149C11-1.jpg" type="text/javascript"><
/script>
<
script src="http://img.readke.com/220416/0611495040-2.jpg" 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="http://www.srcmini.com/#/route/1">route1<
/a>
<
a href="http://www.srcmini.com/#/route/2">route2<
/a>
<
/div>
<
div id="content"><
/div>
<
/body>
<
/html>
输出:
将上面的代码保存在execute.html文件中,然后在新的浏览器中打开该文件。
文章图片
推荐阅读
- backbone.js路由导航navigate
- backbone.js路由route
- backbone.js路由初始化
- backbone.js路由routes
- backbone.js路由器扩展extend
- backbone.js路由教程
- backbone.js集合create
- backbone.js集合fetch
- backbone.js集合clone