无法在ViewModel Knockout中执行函数

时间:2012-09-12 08:47:44

标签: javascript jquery knockout.js

我有一个像这样的网格系统

<div class="section table_section">
<div class="section_inner" id="divTestKoRemoteGrid">
    <div class="title_wrapper" data-bind="with: Grid">
        <h2 data-bind="text: TotalCount">
            Prices</h2>
    </div>
    <div class="section_content">
        <div id="product_list" data-bind="with: Grid">
<!--[if !IE]>start table_wrapper<![endif]-->
<div class="table_wrapper">
    <div class="table_wrapper_inner">
        <table cellpadding="0" cellspacing="0" width="100%">
            <tbody>
                <tr>
                    <th>
                        No.
                    </th>
                    <th>
                        Tier
                    </th>
                    <th>
                    </th>
                </tr>
                <!-- ko foreach: Data -->
                <tr data-bind="css: { second: $index() % 2 }">
                    <td data-bind="text: Id" style="width: 26px;">
                    </td>
                    <td data-bind="text: Tier" style="width: 35px;">
                    </td>
                    <td style="width: 120px;">
                        <div class="actions_menu">
                            <ul>
                                <li><a class="edit" href="">edit</a></li>
                                <li><a class="delete" href="">deny</a></li>
                            </ul>
                        </div>
                    </td>
                </tr>
                <!-- /ko -->
            </tbody>
        </table>
    </div>
</div>
<!--[if !IE]>end table_wrapper<![endif]-->
</div>
<!-- ko with: Grid -->
<div class="pagination" data-bind="with: Page">
    <span class="page_no" data-bind="text: TotalCount"></span>
    <ul class="pag_list" data-bind="foreach: ko.utils.range(1, TotalPage)">
        <li><a href="" data-bind="click: $parent.Goto, css:{current_page: $data == $parent.CurrentPage}">
            <span><span data-bind="text: $data"></span></span></a></li>
    </ul>
</div>
<!-- /ko -->
    </div>
</div>
</div>

和Knockout viewmodel

var my = my || {};

$(function () {

my.PriceListViewModel = (function () {

    var 
        grid = ko.observable(),
        loadGrid = function () {
            my.ListModel.LoadData();
            grid(my.ListModel);
        };
    return {
        Grid: grid,
        LoadGrid: loadGrid
    };
})();

my.ListModel = (function () {
    var 
        data = ko.observableArray([]),
        totalCount = ko.observable(),
        page = ko.observable(),
        goTo = function (d) {
            $.getJSON("/prices/GetByFilterViaJSON?limit=6&page=" + d, function (list) {
                data(list.Data);
                page(list.Page);
            });
        },
        loadData = function () {
            $.ajax({
                type: 'GET',
                url: '/prices/GetByFilterViaJSON',
                dataType: 'json',
                success: function (list) {
                    data(list.Data);
                    page(list.Page);
                    totalCount(list.TotalCount);
                },
                data: { limit: 6 },
                async: false
            });
        };
    return {
        Data: data,
        TotalCount: totalCount,
        Page: page,
        GoTo: goTo,
        LoadData: loadData
    };
})();

my.PageModel = (function () {
    var 
        currentPage = ko.observable(1),
        totalCount = ko.observable(1),
        limit = ko.observable(),
        start = ko.observable(),
        nextPage = ko.observable(1),
        previousPage = ko.observable(1),
        totalPage = ko.observable(1),
        loadData = function (pageData) {
            currentPage(pageData.CurrentPage);
            totalCount(pageData.TotalCount);
            limit(pageData.Limit); 
            start(pageData.Start);
            nextPage(pageData.NextPage);
            previousPage(pageData.PreviousPage);
            totalPage(pageData.TotalPage);
        };
    return {
        CurrentPage: currentPage,
        TotalCount: totalCount,
        Limit: limit,
        Start: start,
        NextPage: nextPage,
        PreviousPage: previousPage,
        TotalPage: totalPage,
        LoadData: loadData
    };

})();

my.PriceListViewModel.LoadGrid();
ko.applyBindings(my.PriceListViewModel);
});

当我尝试执行GoTo函数时(在ListModel中),浏览器无法在点击事件中找到该函数并且无法绑定。

我尝试解决这个问题5天。你觉得有什么问题?

1 个答案:

答案 0 :(得分:1)

问题是您正在访问父级。这会让你像你需要的那样得到Page,而不是网格。要解决此问题,请使用parents[]。它包含当前可用的所有父母,在这种情况下,您需要使用parents[1]

我有一个示例小提琴http://jsfiddle.net/Rynan/fthFE/

还要查看有关淘汰网站http://knockoutjs.com/documentation/binding-context.html

的文档