codecamp

EmberJS 指定URL类型

指定URL类型

路由器还使用浏览器的历史API来定位之前使用的页面。您也可以停用位置API。

Ember.Router.extend({
  location: 'history'
});

在上面的代码中,路由器使用浏览器的历史 API。

例子

<!DOCTYPE html>
   <html>
      <head>
         <title>Emberjs Specifying the URL Type</title>
            <!-- CDN's-->
            <script src="/attachements/w3c/handlebars.min.js"></script>
            <script src="/attachements/w3c/jquery-2.1.3.min.js"></script>
            <script src="/attachements/w3c/ember-template-compiler.js"></script>
            <script src="/attachements/w3c/ember.min.js"></script>
            <script src="/attachements/w3c/ember.prod.js"></script>
            <script src="/attachements/w3c/ember.debug.js"></script>
      </head>
      <body>
        <script type="text/x-handlebars" data-template-name="application">
             <!-- link-to for navigation between the routes -->
             {{#link-to 'authors'}}AuthorInfo{{/link-to}}
             {{#link-to 'books'}}BookInfo{{/link-to}}
             {{outlet}}
          </script>

           <script type="text/x-handlebars" data-template-name="authors">
              <h2>Authors Page </h2>
              <ul>
                 <li>Herbert Schildt</li>
                 <li>Robert Lafore</li>
              </ul>
           </script>

           <script type="text/x-handlebars" data-template-name="books">
              <h2>Books Page</h2>
              <ul>
                <li>Java</li>
                <li>C++</li>
              </ul>
           </script>

           <script type="text/javascript">
             App = Ember.Application.create();

             App.Router.map(function() {
                //refers to the authors template and path refers within page
                this.route('authors', { path: '/' });
                //refers to the books template
               this.route('books');
             });

             Ember.Router.extend({
             //getting the history of the pages are searched before
                location: 'history'
             });
          </script>
    </body>
 </html>

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上述代码保存在 routing_history.html 文件中

  • 在浏览器中打开此HTML文件。

温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }