Backbone.js 有什么地方可以缩短吗?

Backbone.js 有什么地方可以缩短吗?,backbone.js,Backbone.js,有什么地方可以让它变短?代码运行良好,只需调用多个ajax看起来既麻烦又乏味,代码看起来也不那么优雅,如果有人对此有更好的想法,可以共享在主干中编写代码的方法,以便更易于管理,谢谢 更新1: 通过参考Marc idea,我进一步简化了代码,但不确定这是否是正确的方法,任何反馈都会很好 renderControl: function () { var that = this; var _CountryCol = new CountryCol();

有什么地方可以让它变短?代码运行良好,只需调用多个ajax看起来既麻烦又乏味,代码看起来也不那么优雅,如果有人对此有更好的想法,可以共享在主干中编写代码的方法,以便更易于管理,谢谢

更新1:

通过参考Marc idea,我进一步简化了代码,但不确定这是否是正确的方法,任何反馈都会很好

 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'
        });
    },

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

正如@mike所说,使用侦听器

为此,您需要在提取之前添加代码,如:

       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]))
            });
        },
虽然这个解决方案只是将代码从一个地方移动到另一个地方,并没有真正减少代码。但是,将重复代码放入函数将:

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

我可以参考任何样本吗?@Se0ng11主干网的文档:。此外,您应该创建一个
ComboCol
集合,这样您只需获取一次,以及将选择器(
#sta
#mrt
…)作为元素的视图,这些视图将侦听其模型的一些事件。您确实减少了代码,减少了select2的内容,至少它现在看起来比我的代码更优雅
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'}});