これは私がMVVMで行う方法です:
HTML:
<div id="main_pane_add_truck" data-role="view" data-model="APP.models.main_pane_add_truck">
<input id="main_pane_add_truck_customer_id" data-role="combobox" data-placeholder="Type a Customer" data-value-primitive="true" data-text-field="Name" data-value-field="CustomerID"
data-bind="value: customer_id, source: customer_id_ds, events: { change: customer_id_change }" />
</div>
Javascript モデル:
window.APP = {
models: {
main_pane_add_truck: kendo.observable({
customer_id: null,
customer_id_ds: new kendo.data.DataSource({
type: "odata",
transport: {
read: ROOTURL + BODYURL + "MyCustomers"
},
schema: {
model: {
fields: {
CustomerID: { type: "number" },
Name: { type: "string" },
}
}
}
}),
customer_id_change: function customer_id_change(e) {
try {
var found = false;
var combobox = $("#main_pane_add_truck_customer_id").data("kendoComboBox");
var customer_id = e.data.customer_id;
var dataSource = this.get("customer_id_ds");
var data = dataSource.data();
var data_length = data.length;
if (data_length) {
for (var i = 0; i < data_length; i++) {
if (data[i].CustomerID === customer_id) {
found = true;
}
}
if (!found) {
this.set("customer_id", data[0].CustomerID);
combobox.select(0);
}
}
else {
this.set("customer_id", null);
}
}
catch (e) {
console.log(arguments.callee.name + " >> ERROR >> " + e.toString());
}
},
}),
}
};