PagerJS如何构建导航栏? [英] PagerJS how to build a navbar?

查看:51
本文介绍了PagerJS如何构建导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是编写一些可重用的代码来渲染基本的导航栏,因为这将是一个非常重复的任务。以下功能是我的第一个目标:

My goal is to write some reusable code to render basic navbars, since it would be a very repetitive task. The following features are my first goal:


  • 每个页面都应以foreach绑定呈现

  • 每个页面应该抓住活动状态读取当前路径

  • 每个页面应该加载async或inline

这是我的第一次尝试。我希望标记是这样的

Here's my first attempt. I want markup to be something like this

             <ul data-bind='foreach: pages'>
                <li>
                  <!-- 
                   [1]
                   Here a toggler is needed for active/no-active status,
                   i.e. a class binding.
                   -->
                  <a data-bind='html: caption, click: $data.load'></a>
                </li>
              </ul>

每个页面项目应如下所示

Each page item should look something like this

function PageItem(id, caption) {
  this.id= id;
  this.caption = caption;
  this.page = pager.page.find(id);
  this.load = function() {
    // [2]
    // Code here to trigger page load,
    // i.e. this.page.async(someCallback, this.id);
  }
  this.active = function() {
    // [3]
    return this.page.isVisible();
  }
}

使用目标:

function VM() {
  var self = this;
  self.pages = [];
  self.pages.push(new PageItem('dashboard', "<i class='fa-icon-home'></i>"));
  self.pages.push(new PageItem('offerJoin', 'Offer'));
}

var vm = new VM();
pager.extendWithPage(vm)
ko.applyBindings(vm);
pager.start('dashboard');

我需要[1],[2]和[3]主题的帮助。任何指针?

I need help with [1], [2] and [3] topics. Any pointer?

推荐答案

以下是构建它的方法。这只是一个例子。

Here is how you can built it. This is only example.

app的结构就是这样你可以自定义。

The structure of app is like this which you can customize.

app/
   /index.js
   /index.html/
   /lib/
       /pager.js
       /require.js
       /knockout-3.0.0beta.js
   /views/
         /test.html
         /test1.html

以下是你如何做到的。

第一个index.html

First index.html

<html>
    <head>
        <script data-main="index.js" type="text/javascript" src="lib/require.js"></script>
    </head>
<body>
    <div class="container" style="padding-top: 30px;">
    <span id="span" onclick = 'clickme(this)'>I am span</span>
        <div data-bind="page: {id: 'start' , title : 'First Page'}">
            you are currently viewing the content of first page. <br />
            <a  href="#!/start/deep">first child</a><br />
            <a  href="#!/start/second">second child</a><br />
        <br />
        <div data-bind="page: {id: 'deep', title : 'Second Page',role: 'start', source: 'views/test1.html'}">
            you are currently viewing the content of first page inside First Page.
            <br />
            <a data-bind="page-href :'../second'" >Second Child</a>
        </div>  
        <div data-bind="page: {id: 'second', title : 'Second Page', source: 'views/test.html'}">
            you are currently viewing the content of second page inside Second Page.
            <br />
            <a data-bind="page-href :'../deep'" >First Child</a>
        </div>          
        <br />
        <br />
        <br />
        <a  href="#!/structure">Go to Structure</a>
        </div>
        <div data-bind="page: {id: 'structure', title : 'Second Page'}">
            you are currently viewing the content of second page.<br />
            <a  href="#!/start">Go to Start</a>
        </div>
    </div>
</body>
</html>

下一个index.js

Next index.js

requirejs.config({
    shim:{
        bootstrap:['jquery'],
        hashchange:['jquery']
    },
    paths:{
        jquery:'lib/jquery-1.10.2',
        knockout:'lib/knockout-3.0.0beta',
        pager:'lib/pager'
    }
});

requirejs(['jquery','knockout','pager'], function ($, ko,pager) {

    function PagerViewModel(){
        var self    =   this;
    }

    $(function () {
        pager.Href.hash = '#!/';
        pager.extendWithPage(PagerViewModel.prototype);
        ko.applyBindings(new PagerViewModel());
        pager.start();
    });
});

加载的视图

test .html

test.html

<h3>Second Page</h3>
<p>This is a test view loaded by pager.js</p>
<p>The view loads with ajax request when the main page loads</p>
<p>All the pages that need to be loaded are loaded only once with ajax</p>
<p>while navigating the pages are not loaded again</p>

<a data-bind="page-href :'../deep'" href="#">First Child</a>

test1.html

test1.html

<h3>First Page</h3>
<p>This is yet another page loaded by pager.js</p>
<a data-bind="page-href :'../second'" href="#">Second Child</a>

你可以看到我如何创建导航栏,标题是第一个孩子第二个孩子

You can see how i created navigation bar the titles are first child and second child.

您可以下载演示这里

这篇关于PagerJS如何构建导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆