为什么kendo.observable不读取数据源

时间:2016-06-16 16:44:19

标签: javascript kendo-ui kendo-mvvm odata-v4

你能解释为什么kendo ui observable在绑定到html时不读取数据源吗?

我的代码基于此示例:http://demos.telerik.com/kendo-ui/mvvm/remote-binding

我不理解下拉列表与可观察量之间的联系。

InitObservable = function (Id) {
viewModel = kendo.observable({
    //create a dataSource
    tacheDataSource: new kendo.data.DataSource({
        autoSync: true,
        transport: {
            read: {
                url: function () {
                    return crudServiceBaseUrl + "/Taches?ID=" + Id;
                },
                method: "GET",
                dataType: "json"
            }
            ,
            update: {
                url: crudServiceBaseUrl + "/Taches",
                method: "PATCH",
                dataType: "json"
            }
            ,
            destroy: {
                url: crudServiceBaseUrl + "/Taches/Destroy",
                dataType: "json"
            }
            ,
            create: {
                url: crudServiceBaseUrl + "/Taches",
                method: "POST",
                dataType: "json"
            }
            ,
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }
            }
        },
        batch: true,
        pageSize: 20,
        schema: {
            model: {
                id: "ID",
                fields: TacheFields
            }
        }
    }), // endDatasource
    selectedTache: null, //this field will contain the edited dataItem
    hasChange: false,
    save: function (e) {
        this.tacheDataSource.sync();
        this.set("hasChange", false);
    },
    remove: function () {
        if (confirm("Etes vous sûr(e) de vouloir supprimer cette tâche ?")) {
            this.tacheDataSource.remove(this.selectedTache);
            this.set("selectedTache", this.tacheDataSource.view()[0]);
            this.change();
        }
    },
    showForm: function () {
        return this.get("selectedTache") !== null;
    },
    change: function () {
        this.set("hasChanges", true);
    }//,
    //cancel: function () {
    //    this.dataSource.cancelChanges(); //calcel all the change
    //    validator.hideMessages(); //hide the warning messages
    //    $("#tacheWindow").data("kendoWindow").close();
    //}
});
kendo.bind($("#tacheWindow"), viewModel);
}

我使用datasource.read()单独测试了数据源,它可以工作。

读取数据源的触发器是什么?

-----新细节

我添加了

type: "odata-v4"

在数据源中,我更新了架构:

e
schema: {
            data:function(data){
                var toReturn = data.value;
                return toReturn;
            },
            model: {
                id: "ID",
                fields: TacheFields
            }
        }

这就强制读取()

viewModel.selectedTache = proprietesEcranTache.tacheId;
if (viewModel.showForm()) {
    viewModel.tacheDataSource.read();
    kendo.bind($("#tacheWindow"), viewModel);
}

我在chrome的网络调试器中看到了我的答案,我知道我收到的形式没有错误但没有显示数据。

这里是oData回答

{
    "@odata.context":"http://localhost:14986/odata/$metadata#Taches","value":
    [
        {
        "ID":1,"Description":"D\u00e9marrage application","CreateurId":7,"TypeTacheID":1,"EtatTacheID":6,"ValidantId":null,"DateValidation":null,"EstValidee":false,"CommentaireValidation":null,"EvennementPrecedentID":null
        }
    ]
}

这是我的表格

<div id="tacheWindow">
<form id="TacheForm">
    <ul class="TacheFormFields">
        <li class="">
            <div class="formFieldTitle">Id</div>
            <div class="formFieldInput textField"><input id="tacheId" type="text" data-bind="value: ID" /></div>
        </li>
        <li>
                <div class="formFieldTitle">Type de tâche</div>
                <select id="typesTachesDdl" data-role="dropdownlist"
                        data-bind="value: TypeTacheID"
                        data-value-primitive="true"
                        data-text-field="Nom"
                        data-value-field="ID"></select>
            </li>
            <li>
                <div class="formFieldTitle">Description</div>
                <div class="formFieldInput textField">
                    <input type="text" data-bind="value: Description" />
                </div>
            </li>
            <li>
                <div class="formFieldTitle">Createur</div>
                <select id="CreateursDdl" data-role="dropdownlist"
                        data-bind="value: CreateurId"
                        data-value-primitive="true"
                        data-text-field="Nom"
                        data-value-field="ID"></select>
            </li>
            <li>
                <div class="formFieldTitle">Validant</div>
                <select id="ValidantsDdl" data-role="dropdownlist"
                        data-bind="value: ValidantId"
                        data-value-primitive="true"
                        data-text-field="Nom"
                        data-value-field="ID"
                        disabled="disabled"></select>
            </li>
    </ul>

    <div class="dialog_buttons">
        <button id="TacheFormTemplateSave" data-bind="click: observableSave" class="k-button">Ok</button>
        <button id="TacheFormTemplateSave" data-bind="click: observableCancel" class="k-button">Annuler</button>
    </div>
</form>

2 个答案:

答案 0 :(得分:0)

如您所知,将数据源放置在视图模型中只会使其变得可观察,仅此而已。它只会在传递给kendo小部件(例如DropDownList)时被读取。 telerik演示在绑定的html容器中显示:

<select data-role="dropdownlist" data-option-label="Select product"
        data-value-field="ProductID" data-text-field="ProductName"
        data-bind="source: productsSource, value: selectedProduct" style="width: 100%;"></select>

kendo.bind语句扫描html容器中是否有具有data-role属性的元素。在上面的例子中,它将找到data-role =“dropdownlist”,实例化DropDownList小部件并为DOM添加必要的html元素。声明的这一部分:

data-bind="source: productsSource"

...将在视图模型中搜索名为“productsSource”的数据源,并将其作为要使用的数据源分配给DropDownList。然后,DropDownList将触发对该数据源的读取,以便用数据填充自己。

答案 1 :(得分:0)

我创建了一个有效的简单示例

Home Page
<div id="editForm">
    <table>
        <tr>
            <td>Id</td>
            <td>Nom</td>
        </tr>
        <tr>
            <td>
                <input data-role="dropdownlist"
                       data-auto-bind="false"
                       data-text-field="Nom"
                       data-value-field="ID"
                       data-bind="value: selectedPerson,
                              source: persons,
                              visible: isVisible,
                              enabled: isEnabled,
                              events: {
                                change: onChange
                              }"
                       style="width: 200px;" />
            </td>
            <td><input type="text" data-value-update="displaySelectedPerson" data-bind="value: selectedPerson.Nom" class="k-textbox"  /></td>
        </tr>
    </table>
</div>

重要细节:在文本框中:data-bind =&#34; value:selectedPerson.Nom&#34;

这允许observable更新字段。

Javascript:

var persons = [
    { ID: "1", Nom: "Lolo" },
    { ID: "2", Nom: "Toto" }
];

documentReady = function () {
    var viewModel = new kendo.observable({
        personsSource: persons
        , persons: new kendo.data.DataSource({
            data: persons,
            schema: {
                model: {
                    fields: {
                        ID: { type: "number" }
                        , Nom: { type: "string" }
                    }
                }
            }
        })
        , selectedPerson: null
        , hasChange : false
        , isPrimitive: false
        , isVisible: true
        , isEnabled: true
        , primitiveChanged: function () {
            this.set("selectedPerson", null);
        }
        , onChange: function () {
            console.log("event :: change (" + this.displaySelectedPerson() +          ")");
        }
        , displaySelectedPerson: function () {
            var selectedPerson = this.get("selectedPerson");
            return kendo.stringify(selectedPerson, null, 4);
        }
    });
    kendo.bind($("#editForm"), viewModel);
}