利用art.template模仿VUE 一次渲染多个模版

TypeScript代码

 

import template = require('art-template/lib/template-web');

interface TemplateBindConfig {
    el: string
    data: object
}

interface TemplateList {
    els: object
    template: string
}

class TmpBind {

    el: string

    template: any

    data: object

    renderFn: any



    // 构造函数
    constructor(config:TemplateBindConfig) {
        // 绑定的容器id
        this.el = config.el;

        // 注入的数据
        this.data = config.data;

        this.renderFn = null
        var nodes=document.querySelector(config.el).children;
        var i=nodes.length;
        if(i>0){
            while(i--){
                if(nodes[i].tagName.toLowerCase()=="script" && nodes[i].getAttribute("type")=="text/html"){
                    // 模版id
                    this.template = nodes[i].innerHTML;
                    break;
                }
            }
        }

        this.render()
    }

    // 渲染模版
    render(data?): void {
        if (data) {
            this.data = data;
        }
        // 解析模版
        if(!this.renderFn){
            this.renderFn= template.compile(this.template);
        }

        const source = this.renderFn(this.data);
        // 更新容器的值
        document.querySelector(this.el).innerHTML = source;


    }


    setData(value: any): void {
        this.data=value;
        this.render();
    }

    // 重新设置模板
    setTemplate(value: any): void {
        this.template = value;
        this.renderFn= template.compile(value);
        this.render();
    }

    // 获取数据
    getData(): object {
        return this.data;
    }


}
View Code

 

 

 

 

 

 

编译后的JavaScript 

    var TemplateList = /** @class */ (function () {
        function TemplateList() {
        }
        return TemplateList;
    }());
    var TmpBind = /** @class */ (function () {
        // 构造函数
        function TmpBind(config) {
            // 绑定的容器id
            this.el = config.el;
            // 注入的数据
            this.data = config.data;
            this.template = new Array();
            var nodes = document.querySelector(config.el).children;
            TmpBind.getTemplates(nodes, this);
            this.render();
        }
        TmpBind.getTemplates = function (nodes, instance) {
            var i = nodes.length;
            if (i > 0) {
                while (i--) {
                    if (nodes[i].tagName.toLowerCase() == "script" && nodes[i].getAttribute("type") == "text/html") {
                        // 模版id
                        var ts = new TemplateList();
                        ts.el = nodes[i];
                        ts.template = nodes[i].innerHTML;
                        ts.renderFn = template.compile(ts.template);
                        ts.source = ts.renderFn(instance.data);
                        ts.els = new Array();
                        instance.template.push(ts);
                    }
                    if (nodes[i].children && nodes[i].children.length > 0) {
                        TmpBind.getTemplates(nodes[i].children, instance);
                    }
                }
            }
        };
        // 渲染模版
        TmpBind.prototype.render = function (data) {
            this.reset();
            if (data) {
                this.data = data;
            }
            for (var i = 0; i < this.template.length; i++) {
                var div1 = document.createElement("div");
                this.template[i].source = this.template[i].renderFn(this.data);
                div1.innerHTML = this.template[i].source;
                this.template[i].els = new Array();
                while (div1.childNodes.length > 0) {
                    this.template[i].els.push(div1.childNodes[0]);
                    this.template[i].el.parentNode.insertBefore(div1.childNodes[0], this.template[i].el);
                }
            }
            document.querySelector(this.el).template = this.template;
        };
        // 重置
        TmpBind.prototype.reset = function () {
            var template = document.querySelector(this.el).template;
            if (template) {
                for (var i = 0; i < template.length; i++) {
                    if (template[i].els && template[i].els.length > 0) {
                        var j = template[i].els.length;
                        while (j--) {
                            template[i].els[j].parentNode.removeChild(template[i].els[j]);
                        }
                    }
                }
            }
        };
        // 设置数据
        TmpBind.prototype.setData = function (value) {
            this.data = value;
            this.render();
        };
        // 获取数据
        TmpBind.prototype.getData = function () {
            return this.data;
        };
        return TmpBind;
    }());
View Code

 

 

 

 

 

示例: 

 

<div id="div2">
    这是第一个模版......
    <script type="text/html">
        <p>{{msg}}</p>
        <ul>
            {{each list as item}}
            <li>{{item}}</li>
            {{/each}}
        </ul>
    </script>
    二个模版
    <script type="text/html">

        <ul>
            {{each list as item}}
            <li>{{item}}</li>
            {{/each}}
        </ul>
    </script>
    N个模版
    <script type="text/html">

        <ul>
            {{each list as item}}
            <li>{{item}}</li>
            {{/each}}
        </ul>
    </script>
</div>
<script>
    var vm = new TmpBind({
        el: "#div2",
        data: {
            msg: "欢迎来到模版绑定的世界",
            list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
        }
    })
    // 获取数据
    var data = vm.getData();
    // 更改数据
    data.msg = "欢迎来到模版绑定的世界3333333333"
    data.list.push(333)

    setTimeout(function () {
        // 设置数据
        vm.setData(data)
    }, 3000)

</script>

 

 

 

 

 

 

 

如果这篇文章对您有帮助,您可以打赏我

 

技术交流QQ群:15129679

 

 
 

 

转载于:https://www.cnblogs.com/yeminglong/p/10839107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值