ember.js101 入门教程 第七课

Ember 经过管理视图层次结构,大大减小了应用程序代码,这须要经过您的路由配置和模板进行。这里是一个简单的应用程序,显示一个login 页面和一个about 页面,点击跳转一些路由和模版。javascript

嵌套路由得例子展现

一、没有选择连接时,用 index 模版渲染输出:css


_________________________________________________________________________________________________
二、选择about或login连接是导航到子页面html


_________________________________________________________________________________________________
三、选择about子页面location连接时,嵌套路由自动导航到about/location模版java


_________________________________________________________________________________________________jquery

四、选择about子页面product连接时,嵌套路由自动导航到about/product模版ajax

这是定义的路由:this

var App = Ember.Application.create();

App.Router.map(function() {
  this.resource('about');
  this.resource('login');
});

这是定义的模版:code

<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。咱们能够添加这些做为顶级路由;cdn

this.resource('aboutLocation', {path: '/about/location'});
this.resource('aboutProduct', {path: '/about/product'});

about的模版应该是这个样子htm

<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>
相关文章
相关标签/搜索