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