骨干取,哪里可以缩短?

时间:2013-06-20 09:06:42

标签: backbone.js

 renderControl: function () {
        var that = this;
        var _CountryCol = new CountryCol();
        var _ComboCol = new ComboCol();

        _CountryCol.fetch({
            success: function (data) {
                that.$("#ctr").select2({
                    placeholder: "Select a Country",
                    allowClear: true,
                    data: JSON.parse(JSON.stringify(data)),
                });
            }
        });
        _ComboCol.fetch({
            data: { id: 'status' },
            success: function (data) {
                that.$("#sta").select2({
                    placeholder: "Select a status",
                    allowClear: true,
                    data: JSON.parse(JSON.stringify(data)),
                });
            }
        });
        _ComboCol.fetch({
            data: { id: 'marital' },
            success: function (data) {
                that.$("#mrt").select2({
                    placeholder: "Select a marital",
                    allowClear: true,
                    data: JSON.parse(JSON.stringify(data)),
                });
            }
        });
        _ComboCol.fetch({
            data: { id: 'daytype' },
            success: function (data) {
                that.$("#dty").select2({
                    placeholder: "Select a type",
                    allowClear: true,
                    data: JSON.parse(JSON.stringify(data)),
                });
            }
        });
        this.$("#hpn, #hmn, #icn").numeric({ decimal: false, negative: false });
        this.$('#dob').datepicker({
            dateFormat: 'dd/mm/yy'
        });
    },

任何地方缩短?代码工作正常,只需调用多个ajax看起来麻烦又乏味,代码看起来也不那么优雅,如果有人对它有更好的想法,可以分享骨干代码的方式,以便更容易管理,感谢

更新1:

通过参考Marc的想法,我进一步减少了代码,但不确定这是否正确,任何反馈都会很棒

       renderControl: function () { //use to render special control
            var that = this;
            var _CountryCol = new CountryCol();
            var _ComboCol = new ComboCol();
            $.when(
                _CountryCol.fetch(),
                _ComboCol.fetch({ data: { id: 'status' } }),
                _ComboCol.fetch({ data: { id: 'marital' } }),
                _ComboCol.fetch({ data: { id: 'daytype' } })).done(
                function (country, status, marital, daytype) {
                    that.populateSelect('#ctr', "Select a country", country);
                    that.populateSelect("#sta", "Select a status", status);
                    that.populateSelect("#mrt", "Select a marital", marital);
                    that.populateSelect("#dty", "Select a type", daytype); 
            });

            this.$("#hpn, #hmn, #icn").numeric({ decimal: false, negative: false });
            this.$('#dob').datepicker({
                dateFormat: 'dd/mm/yy'
            });
        },
        populateSelect: function (selector, placeholder, collection) {
            debugger;
            this.$(selector).select2({
                placeholder: placeholder,
                allowClear: true,
                data: JSON.parse(JSON.stringify(collection[0]))
            });
        },

2 个答案:

答案 0 :(得分:2)

AS @mike说,请使用听众。

要执行此操作,您需要在获取之前添加代码,如:

_CountryCol.on('sync', function(collection) {
  that.$("#ctr").select2({
    placeholder: "Select a Country",
    allowClear: true,
    data: JSON.stringify(collection.toJSON())
  });
});
_CountryCol.fetch()

虽然这个解决方案只是将代码从一个地方移动到另一个地方,但并没有真正削减代码。将重复的代码放在一个函数中将会:

populateSelect = function(selector, placeholder, collection) {
  that.$(selector).select2({
    placeholder: placeholder,
    allowClear: true,
    data: JSON.stringify(collection.toJSON())
  });
}

_CountryCol.on('sync', function(collection) {
  populateSelect("#ctr", "Select a Country", collection);
});
_CountryCol.fetch();

_ComboCol.on('sync', function(collection) {
  populateSelect("#sta", "Select a status", collection)
});
_ComboCol.fetch({data: {id: 'status'}});

答案 1 :(得分:1)

不确定这里的整体情况,但通常在Backbone中,最好处理对集合中侦听器的提取的响应,而不是fetch方法本身的回调。这样你可能只需要编写一次回调代码。