Ember 通过管理视图层次结构,大大减少了应用程序代码,这需要通过您的路由配置和模板进行。这里是一个简单的应用程序,显示一个login 页面和一个about 页面,点击跳转一些路由和模版。
嵌套路由得例子展示
1、没有选择链接时,用 index 模版渲染输出:
_________________________________________________________________________________________________
2、选择about或login链接是导航到子页面
_________________________________________________________________________________________________
3、选择about子页面location链接时,嵌套路由自动导航到about/location模版
_________________________________________________________________________________________________
4、选择about子页面product链接时,嵌套路由自动导航到about/product模版
这是定义的路由:
var App = Ember.Application.create();
App.Router.map(function() {
this.resource('about');
this.resource('login');
});
这是定义的模版:
<script type="text/x-handlebars">
<h1>Application</h1>
<ul>
<li>{{#linkTo "about"}}About{{/linkTo}}</li>
<li>{{#linkTo "login"}}Login{{/linkTo}}</li>
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" id="about">
<h2>About</h2>
</script>
<script type="text/x-handlebars" id="login">
<h2>Login</h2>
</script>
如果在about 页面上,我们想渲染一些子页面吗?例如,我们可以渲染 about/location 和 about/product。我们可以添加这些作为顶级路由;
this.resource('aboutLocation', {path: '/about/location'});
this.resource('aboutProduct', {path: '/about/product'});
about的模版应该是这个样子
<h2>About</h2>
<ul>
<li>
{{#linkTo "aboutLocation"}}Location{{/linkTo}}
</li>
<li>
{{#linkTo "aboutProduct"}}Product{{/linkTo}}
</li>
</ul>
<h3>Location</h3>
这是有问题的,因为子页面导航完全依赖于{{#linTo}},我们当然不想对于没个跳转的页面重复的设置导航链接。幸运的是,Ember支持嵌套的路由和模板,Ember可以自动管理它。
App.Router.map(function() {
this.resource('about', function() {
this.route('location');
this.route('product');
});
this.resource('login');
});
然后,我们的模板调整为(about/location, about/product)。然后,我们在模板中添加一个{{outlet}}块语句。最后的结果看起来像:
<script type="text/x-handlebars" id="about">
<h2>About</h2>
<ul>
<li>
{{#linkTo "about.location"}}Location{{/linkTo}}
</li>
<li>
{{#linkTo "about.product"}}Product{{/linkTo}}
</li>
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" id="login">
<h2>Login</h2>
</script>
<script type="text/x-handlebars" id="about/location">
<h3>Location</h3>
</script>
<script type="text/x-handlebars" id="about/product">
<h3>Product</h3>
</script>
通过嵌套路由的设置,可以看到,ember可以实现about路由跳转到about/location和about/product,对于网址我们没有配置类似于about/location的路径,但该网址可以明白到整个视图层次,进行跳转。最后,你可以定义index模板,当你选择了about链接但没有选择location或product子链接时,将被渲染到这个模板的输出,注意不要和上一级模板里的index混淆,是两个不同层级的模版,虽然显示的都是index,这是实在about的下级,前一个顶级模版的下级。
<script type="text/x-handlebars" id="about/index">
<h3>Index</h3>
</script>
完整代码:
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2016 by symphonyh (http://jsbin.com/diboye/1/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ember.js/1.0.0-rc.2/ember.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<style id="jsbin-css">
body {
font-family: sans-serif;
color: #454545;
}
a {
color: blue;
}
a.active {
color: red;
}
</style>
</head>
<body>
<script type="text/x-handlebars">
<h1>Application</h1>
<ul>
<li>{{#linkTo "about"}}About{{/linkTo}}</li>
<li>{{#linkTo "login"}}Login{{/linkTo}}</li>
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" id="about">
<h2>About</h2>
<ul>
<li>
{{#linkTo "about.location"}}Location{{/linkTo}}
</li>
<li>
{{#linkTo "about.product"}}Product{{/linkTo}}
</li>
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" id="index">
<h2>Index</h2>
</script>
<script type="text/x-handlebars" id="login">
<h2>Login</h2>
</script>
<script type="text/x-handlebars" id="about/index">
<h3>Index</h3>
</script>
<script type="text/x-handlebars" id="about/location">
<h3>Location</h3>
</script>
<script type="text/x-handlebars" id="about/product">
<h3>Product</h3>
</script>
<script id="jsbin-javascript">
var App = Ember.Application.create();
App.Router.map(function() {
this.resource('about', function() {
this.route('product');
this.route('location');
});
this.resource('login');
});
</script>
</body>
</html>