将自定义函数添加到kendo可观察数组中

时间:2015-02-03 16:25:15

标签: kendo-ui kendo-observable

如何添加自定义或扩展kendo可观察数组? JS小提琴:http://jsfiddle.net/balarvs/mctyswh2/3/

$(document).ready(function () {
/*   var ar = [
    { id: "10" }, 
    { id: "12" },
    { id: "20" }
]; */

if (![].getById) {
    Array.prototype.getById = function (id) {
        'use strict';
        var arr = Object(this);
        var found;
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i];
            if (item.id == id) {
                found = item;
            }
        }
        return found;
    };
}
var obser=new kendo.data.ObservableArray( [
    { id: "10" },
    { id: "12" },
    { id: "20" }
]);
var bb = obser.getById("20");
if (bb != undefined) console.log(bb.id);
});

1 个答案:

答案 0 :(得分:0)

您应该扩展Arraykendo.data.ObservableArray。你的代码应该是这样的:

$(document).ready(function () {
    kendo.data.ObservableArray.prototype.getById = function (id) {
        'use strict';
        var arr = Object(this);
        var found;
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i];
            if (item.id == id) {
                found = item;
            }
        }
        return found;
    };

    var obser=new kendo.data.ObservableArray( [
        { id: "10" },
        { id: "12" },
        { id: "20" }
    ]);
    var bb = obser.getById("23");
    if (bb != undefined) console.log(bb.id);
});

您的JSFiddle在此修改:http://jsfiddle.net/OnaBai/mctyswh2/8/