Ruby's Louvre

每天学习一点点算法

导航

统计

webpack打包avalon+mmRouter

这是上一篇《webpack打包avalon+oniui+jquery》的姐妹篇,avalon 的高级应用篇。大家要知道,现在最流行的网页架构就是SPA,SPA能提高用户体验。用户许多数据都在保存到本地,第二次跳回此子页时,不用重新远程请求数据与页面。

avalon的mmRouter吸收了 backbone.History与angular路由器模块的优点,简单易用,性能优良(router匹配方法使用字典树实现,比纯正则匹配快许多)。

我们沿用上一篇的目录,在dev下建立一个mmRouter目录,将mmRouter用到的两个模板引进来

这次我们添加了两个页面,就需要添加两个入口JS文件。webpack里面也做了相应调整,并且使用webpack.optimize.CommonsChunkPlugin将共用部分抽取出来

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中的对应属性,进行切换!

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.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/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>

再看另一个页面

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命令

posted on 2015-11-19 19:55  司徒正美  阅读(3976)  评论(2编辑  收藏