如何将下面的代码优化一下

发布于 2022-09-06 03:24:32 字数 4300 浏览 27 评论 0

代码中重复的遍历有点多
如何减少重复,其中每个._find是返回了data对象中elementCode为固定值的数组

var moduleModel = new Bb.Model({});
                    var data1 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'usagetype'
                    });
                    var data2 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'policystatus'
                    });
                    var data3 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'riskname'
                    });
                    var data4 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'businessnature'
                    });
                    var data5 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'chnltype'
                    });
                    var data6 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'paymethod'
                    });
                    var data7 = _.find(m.get('data'), function(d) {
                        return d.elementCode == 'traveltaxmark'
                    });
                    if (data1.dataList && data2.dataList && data3.dataList && data4.dataList && data5.dataList && data6.dataList && data7.dataList) {
                        moduleModel.set('usagetype', data1.dataList);
                        moduleModel.set('policystatus', data2.dataList);
                        moduleModel.set('riskname', data3.dataList);
                        moduleModel.set('businessnature', data4.dataList);
                        moduleModel.set('chnltype', data5.dataList);
                        moduleModel.set('paymethod', data6.dataList);
                        moduleModel.set('traveltaxmark', data7.dataList);

                        that.showChildView('policystatus', new policystatusView({
                            model: moduleModel,
                            el: that.getUI('policystatus')
                        }));
                        that.getChildView('policystatus').render();

                        that.showChildView('businessnature', new businessnatureView({
                            model: moduleModel,
                            el: that.getUI('businessnature')
                        }));
                        that.getChildView('businessnature').render();

                        that.showChildView('chnltype', new chnltypeView({
                            model: moduleModel,
                            el: that.getUI('chnltype')
                        }));
                        that.getChildView('chnltype').render();

                        that.showChildView('traveltaxmark', new traveltaxmarkView({
                            model: moduleModel,
                            el: that.getUI('traveltaxmark')
                        }));
                        that.getChildView('traveltaxmark').render();

                        that.showChildView('paymethod', new paymethodView({
                            model: moduleModel,
                            el: that.getUI('paymethod')
                        }));
                        that.getChildView('paymethod').render();

                        that.showChildView('usagetype', new usagetypeView({
                            model: moduleModel,
                            el: that.getUI('usagetype')
                        }));
                        that.getChildView('usagetype').render();

                        that.showChildView('riskname', new risknameView({
                            model: moduleModel,
                            el: that.getUI('riskname')
                        }));
                        that.getChildView('riskname').render();
                    } else {
                        layer.alert('查询失败!');
                        return false;
                    }

                },
                error: function(e) {
                    layer.close(idx);
                    if (e.responseText === 'logout') {
                        window.location.href = '/login.html';
                    }
                    layer.alert('请求失败!');
                }
   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2022-09-13 03:24:32
var data = {
    "usagetype": null,
    "policystatus": null,
    "riskname": null,
    "businessnature": null,
    "chnltype": null,
    "paymethod": null,
    "traveltaxmark": null
}

var mData = m.get('data')

var viewFactory = (type, param) => {
    switch (type) {
        case 'usagetype':
            return new usagetypeView(param)
    }
}

Object.keys(data).map(key => {
    var dataList = (data[key] = _.find(mData, d => d.elementCode == key))['dataList']
    dataList &&
    moduleModel.set(key, dataList)

    that.showChildView(key, viewFactory(key, {
        model: moduleModel,
        el: that.getUI(key)
    }))

    that.getChildView(key).render();
})

工厂方法我没写全,也没跑过,大概意思吧,也不知道适不适合你的代码环境

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文