1
 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]))
            });
        },
4

2 に答える 2

2

@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'}});
于 2013-06-20T09:47:19.823 に答える
1

ここで全体像についてはわかりませんが、通常、バックボーンでは、fetchメソッド自体のコールバックではなく、コレクションのリスナーでフェッチへの応答を処理することが望ましいです。こうすれば、おそらくコールバック コードを 1 回記述するだけで済みます。

于 2013-06-20T09:10:57.583 に答える