avalon 在组件内调用另一个组件,异步获取数据会出现不能删除<xmp> 标签,同步没问题

Kevin
  • 1
新手上路,请多包涵

js 代码

function heredoc(fn) {
        return fn.toString().replace(/^[^\/]+\/\*!?\s?/, '').
        replace(/\*\/[^\/]+$/, '').trim().replace(/>\s*</g, '><')
    }
    var data = [{
            id: '1',
            title: "test1",
            children: [{
                id: "11",
                title: "test11",
                children: [{
                    id: "21",
                    title: "test21"
                }]
            }]
        },
        {
            id: '2',
            title: "test2"
        }
    ]

    var vm = avalon.define({
        $id: "test",
        treelist: []
    })

    setTimeout(function () {
        vm.treelist = data;
    }, 2000)
    avalon.component('tree', {
        template: heredoc(function () {
            /*
            <div>
                <ul>
                    <li ms-for="(index, el) in @tree | get(0)" ms-class="['test', el.id == 1 && 'test2']" ms-click='@openSubTree(el)'>
                        <a ms-attr="[{href:el.title},(el.children != null && el.children.length > 0) && {title:el.title}]">{{el.title}}</a>
                        <xmp  ms-widget="{is:'tree-child', treeItem: el}"></xmp>
                    </li>
                </ul>
             </div>
             */
        }),
        defaults: {
            tree: [],
            openSubTree: function (el) {
                el.open = !el.open
            }
        }
    })

    avalon.component('tree-child', {
        template: heredoc(function () {
            /*
             <ul class='child-item'>
                <li ms-for="(index, el) in @treeItem.children | get(0)">
                    <span ms-click='@openSubTree(el)'>{{el.title}}</span>
                    <xmp  ms-widget="{is:'tree-child', treeItem: childitem}"></xmp>
                </li>
             </ul>
             */
        }),
        defaults: {
            treeItem: {},
            openSubTree: function (el) {
                el.open = !el.open
            }
        }
    })

html 代码

<body ms-controller="test">
    <xmp  ms-widget="{is:'tree',tree:@treelist}"></xmp>
</body>

标签删不掉

回复
阅读 672
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏