webpack打包avalon+mmRouter

這是上一篇《webpack打包avalon+oniui+jquery》的姐妹篇,avalon 的高級應用篇。你們要知道,如今最流行的網頁架構就是SPA,SPA能提升用戶體驗。用戶許多數據都在保存到本地,第二次跳回此子頁時,不用從新遠程請求數據與頁面。javascript

avalon的mmRouter吸取了 backbone.History與angular路由器模塊的優勢,簡單易用,性能優良(router匹配方法使用字典樹實現,比純正則匹配快許多)。css

咱們沿用上一篇的目錄,在dev下創建一個mmRouter目錄,將mmRouter用到的兩個模板引進來html

此次咱們添加了兩個頁面,就須要添加兩個入口JS文件。webpack裏面也作了相應調整,而且使用webpack.optimize.CommonsChunkPlugin將共用部分抽取出來java

var webpack = require("webpack");

var path = require("path");
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');

module.exports = {
    entry: {
        index: './dev/index', //咱們開發時的入口文件
        router: './dev/router',
        router2: './dev/router2'
    },
    output: {
        path: path.join(__dirname, "dist"), 
        filename: "[name].js"}, //頁面引用的文件
    module: {
        loaders: [
            {test: /\.css$/, loader: 'style-loader!css-loader'}
        ],
        preLoaders: [
            {test: /\.js$/, loader: "amdcss-loader"}
        ]
    },
    plugins: [commonsPlugin],
    resolve: {
        extensions: ['.js', "", ".css"],
        alias: {
            jquery: path.join(__dirname, 'dev/jquery/jquery.js'),
            avalon: path.join(__dirname, 'dev/avalon/avalon.shim'), //在正常狀況下咱們以CommonJS風格引用avalon,以require('avalon')
            '../avalon': path.join(__dirname, 'dev/avalon/avalon.js')//因爲oniui都以是../avalon來引用avalon的,須要在這裏進行別名
        }
    }
}

咱們來看第一個頁面,是作一個切換卡。當咱們點擊連接時,改變地址欄,而後mmHistory被監聽到,通知mmRouter在全部路由規則中進行匹配,匹配到了,就進入對應回調,修改VM中的對應屬性,進行切換!jquery

var avalon = require("avalon")
require("./mmRouter/mmRouter")
var vm = avalon.define({
    $id: "test",
    tabs: [
        {trigger: "切換卡1", panel: "面板1"},
        {trigger: "切換卡2", panel: "面板2"},
        {trigger: "切換卡3", panel: "面板3"}
    ],
    currentIndex: 1
});
avalon.router.get("/:id", function (a) {
    avalon.log(typeof a, a, "這在回調裏")
    vm.currentIndex = a
});

avalon.history.start();
avalon.scan(0, vm);
setTimeout(function(){
    avalon.router.navigate("1")//默認切換到中間項
},400)

router.htmlwebpack


<!DOCTYPE html>
<html>
    <head>
        <title>mmRouter組件</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <script src="dist/common.js"></script>
        <script src="dist/router.js"></script>
        <style>
            .tabs{
                list-style: none;
                margin: 0px;
                padding: 0px;
            }
            .tabs li{
                float: left;
                display: inline-block;
                width:200px;
                height:30px;
                line-height: 30px;
                text-align: center;
                background: green;
                border-left: 1px dashed darkkhaki;
            }
            .tabs li a{
                cursor: pointer;
                text-decoration: none;
                display: inline-block;
                height: 100%;
                width: 100%;
                color: #999;
            }
            .tabs li.active{
                background: goldenrod;
            
            }
            .tabs li.active a{
                color:#fff;
            
            }
            .tabs li:first-child{
                border-left: 0 none;
            }
            .tabs:after{
                content: "";
                clear: both;
                display: table;
                
            }
            .panel{
                width:604px;
                height:120px;
                background: floralwhite;
            }
        </style>
    </head>
    <body ms-controller="test">
        <h1>mmRouter綜合實例一: avalon.get</h1>
        <ul class="tabs">
            <li ms-repeat="tabs" ms-class="active:$index == currentIndex">
                <a ms-href="#!/{{$index}}">{{el.trigger}}</a>
            </li>
        </ul>
        <div class="panel" ms-repeat="tabs" ms-visible="$index == currentIndex">{{el.panel}}</div>
       
    </body>
</html>

再看另外一個頁面web

var avalon = require("avalon")
require("./mmRouter/mmRouter")
var vm = avalon.define({
    $id: "test",
    path: ""
});
avalon.router.get("/:id/*path", function (a) {
    avalon.log(typeof a, a, "math more")
    vm.path = this.path
});
avalon.router.get("/:id", function (a) {
    avalon.log(typeof a, a, "這在回調裏")
    vm.path = this.path
});
avalon.history.start();
avalon.scan(0, vm);

router2.html架構

<!DOCTYPE html>
<html>
    <head>
        <title>mmRouter組件</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <script src="dist/common.js"></script>
        <script src="dist/router2.js"></script>

    </head>
    <body ms-controller="test">
        <ul>
            <li><a href="#!/aaa">aaa</a></li>
            <li><a href="#!/bbb">bbb</a></li>
            <li><a href="#!/ccc">ccc</a></li>
            <li><a href="#!/ddd/222">ddd</a></li>
            <li><a href="#!/eee">eee</a></li>
        </ul>
        <h1>{{path}}</h1>
    </body>
</html>

而後運行webpack命令性能

相關文章
相關標籤/搜索