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>