Kendo Grid在插入网格之前修改远程数据

时间:2014-10-09 17:49:49

标签: javascript json kendo-ui getjson

我有一个像这样设置的视图模型

$(function () {

data = {
    id: "",
    blah: null,
    blah2: null
};

vM = kendo.observable({
    arrData: [],
    DisplayThisArr: [],
    /*
    * Functions you see are here
    */

}); // End vM

kendo.bind($("#grid"), vM);

});

我将它绑定到一个看起来像这样的视图

<!DOCTYPE html>
<html>
<head>
<!-- Imports here, including the vM.js file -->

</head>


<body>
    <div id="container">

        <!-- Set up the Grid -->
        <div id="grid"
             data-role="grid"
             data-column-menu="true"
             data-filterable="true"
             data-sortable="true"
             data-row-filter="true"
             data-columns='[
                                {"field": "1", "title" : "1"},
                                {"field": "2", "title" : "2"},
                                {"field": "3", "title": "3"},
                                {"field": "4", "title": "4"},
                                {"field": "5", "title": "5"},
                                {"field": "6", "title": "6"},
                                {"field": "7", "title": "7"},
                                {"field": "propertyImAdding", "title" : "NEW PROP"}
                            ]'
             data-bind="source: dataSource">
        </div><!-- End of the grid Div -->
    </div><!-- End of the container Div -->
</body>
</html>

我从远程API中提取数据

dataSource: new kendo.data.DataSource({
        schema: {
            model: {
                1: { type: "string" },.
                2: { type: "string" },
                3: { type: "string" },
                4: { type: "string" },
                5: { type: "string" },
                6: { type: "string" },
                7: { type: "string" },
                propertyImAdding: { type: "number" },
            }
        },
        batch: true,
        transport: {
            read:
                function(options) {
                  $.getJSON( 'https://localhost:port/api/blah/blah', data, function( json ) {
                        DisplayThisArr = json;
                        vM.getOtherData();
                        //options.success(???); //IDK what to do with this but I need it somewhere
                    });
                    //options.success(???); //IDK what to do with this but I need somewhere
                }
        }
    })

然后我从另一个api

获取数据
getOtherData: function() {

        $.ajax({
            url: 'https://localhost:port/api/different/blah',
            dataType: 'json',
            async: false,
            success: function(jsonPayload) {
                arrData = jsonPayload;
                vM.modData(arrData, DisplayThisArr)
            }
        });

    }, //end displayData()

然后我修改第一个数组DisplayThisArray以获得我从第二个数组arrData收集的另一个属性。

modData: function(arrData, DisplayThisArr) {

        /* Hidden b/c not relivant */

        /* I add the new property that wasn't in the json data returned from the api and data to DisplayThisArr HERE */
        /* Now DisplayThisArr has been modified */

    }, //END modData()

现在我修改了数据如何在网格中显示它?

[编辑] 在@Brett评论之后,我将它设置为这样,现在我得到了Uncaught TypeError: Cannot read property 'success' of undefined。为什么options未定义?

dataSource: new kendo.data.DataSource({
        schema: {
            model: {
                1: { type: "string" },.
                2: { type: "string" },
                3: { type: "string" },
                4: { type: "string" },
                5: { type: "string" },
                6: { type: "string" },
                7: { type: "string" },
                propertyImAdding: { type: "number" },
            }
        },
        batch: true,
        transport: {
            read:
                function(options) {

                $.ajax({
                  url: 'https://localhost:port/api/blah/blah',
                  dataType: 'json',
                  async: false,
                  data: data,
                  success: function(json) {
                        DisplayThisArr = json;
                        vM.getOtherData();
                        options.success(DisplayThisArr);
                  }
                });//end ajax
            }
        }
    })//End datasource

0 个答案:

没有答案