2

我正在使用来自 Knockout.JS 站点 ( http://jsfiddle.net/rniemeyer/QSRBR/ ) 的分页网格示例来构建网格。我在网格模板中声明了一个点击事件,如下所示(点击:goBackward):-

templateEngine.addTemplate("ko_negrid_navigation", "\
                    <div class=\"ko-grid-pageLinks\">\
                        <br/>\
                        <span><img src=\"/OCSENET/images/bluLeftNew.gif\" style=\"cursor:hand;\" alt=\"Backward\" data-bind='click: goBackward, visible: shouldShowBack'  /></span>\
                        <span>&nbsp;</span>\
                        <span><img src=\"/OCSENET/images/bluRightNew.gif\" style=\"cursor:hand;\" alt=\"Forward\"  data-bind='click: goForward, visible: shouldShowFwd'  /></span>\
                    </div>");

现在我需要在调用此 Grid ViewModel 的 ViewModel 中定义此 goBackward 方法,而不是在此 Grid View Model 自身中定义此方法。当我这样做时,事件不会触发 goBackward()。我正在从另一个 TestViewModel 调用网格,如下所示:

/*----------------------------------------------------------------------*/
/* TEST View Model
/*----------------------------------------------------------------------*/
function TESTViewModel() {
    var self = this;

    self.iRowFrom = ko.observable(1);
    self.iRowTo = ko.observable(5);
    self.gridRowCount = ko.observable(5);

    self.VM_AHISGrid;
    self.GridItems = ko.observableArray([]);    

    //fnDesignGrid
    self.fnDesignGrid = function () {
        self.VM_AHISGrid = new ko.NEGrid.viewModel({
            data: self.GridItems,
            columns: [
                        { headerText: "Address", rowText: "AHIS_Address" },
                        { headerText: "City", rowText: "AHIS_Addr_City" },
                        { headerText: "State", rowText: "AHIS_Addr_State" },
                        { headerText: "Zip", rowText: "AHIS_Addr_Zip" }
                    ],
            pageSize: self.gridRowCount(),
            gridName: 'ALST_NE_GRID'
        });
    }

    // I want this method to be triggered.
    self.goBackward = function () {
        alert('I m Backward')
    };  

}
4

2 回答 2

1

好的,我修好了。很简单,只需定义调用viewmodel的方法如下,它就像魅力一样。

self.NEGridViewModel.goBackward = function () {
        alert('I m Backward')
    };
于 2013-06-25T12:32:55.137 回答
0

如果您的网格是您的视图模型的子级,那么在您的寻呼机模板中,您需要执行类似click: $parent.goBackwardclick: $root.goBackward绑定到位于更高范围级别的函数的操作。

于 2013-06-24T14:47:50.510 回答