Knockout.js不想绑定我的按位置分组的记录列表 [英] Knockout.js not wanting to bind my list of records that has been grouped by location

查看:81
本文介绍了Knockout.js不想绑定我的按位置分组的记录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了好几天了,没有运气.我正在构建一个ASP.NET MVC 5应用程序.我正在为一家餐厅预订应用程序.这个想法是通过使用linq将其位置提取到实体的天数预订组中,然后通过signalR将其发送到客户端.在客户端上,我想将此组合查询与kickout.js绑定,然后显示它,这就是所有错误的地方.将分组的保留发送到客户端可以很好地工作,但是我似乎无法使用剔除功能进行映射.请帮忙.

I have been trying for a few days now with no luck. I'm building a ASP.NET MVC 5 application. I'm building a reservations application for a restaurant. The idea is to extract a days reservations group it by location with linq to entities and then send it with signalR to the client side. On the client side I want to bind this grouped query with knockout.js and then display it, and that is where everything goes wrong. Sending the grouped reservations to the client side works fine but I can't seem to make the mapping with knockout work. Please help.

服务器端模型

var Reservations = db.BistroReservations_Reservations
                                       .GroupBy(reservation => reservation.BistroReservations_Location.Description)
                                       .OrderBy(reservation => reservation.Key.ToString()).ToList();


var context = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<ReservationsHub>();
        context.Clients.All.TestingGroupedReservations(Reservations);

客户端模型

var ReservationsViewModel = function () {
var self = this;

var connection = $.hubConnection();
    var hub = connection.createHubProxy('reservationsHub') 

     var GroupedReservations = ko.mapping.fromJS(reservations);
//Testing -map a collection object to a observalbe and display it underneath the webpage
    hub.on('TestingGroupedReservations', function (reservation) {
        ko.mapping.fromJS(reservation, GroupedReservations);
    });

}

ko.applyBindings(new ReservationsViewModel());  

客户端视图端的代码

  <table class="table" data-bind="visible: !loading()">
                <thead class=".h1 glyphicon-bold">Reservations of Selected Day</thead>
                <tbody data-bind="foreach: GroupedReservations">
                    <tr>
                        <td>Shift</td>
                        <td>
                            <table data-bind="foreach:$data">
                                <tbody>
                                    <tr>
                                        <td data-bind="text:BistroReservations_GuestID"></td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>

推荐答案

从第一次脸红开始,您就永远不会设置GroupedReservations属性,也不会返回自身.如果您不return self,则所有财产均被视为私有财产.

From first blush you are never setting the GroupedReservations property and you are also not returning self. If you do not return self all of your properties are considered private.

此外,您希望公开访问的所有内容都必须是self上的属性.

In addition anything you want to be publicly accessible needs to be a property on self.

var ReservationsViewModel = function () {
var self = this;

var connection = $.hubConnection();
    var hub = connection.createHubProxy('reservationsHub') 

         **self.GroupedReservations  = ko.mapping.fromJS(reservations);**
//Testing -map a collection object to a observalbe and display it underneath the webpage
    hub.on('TestingGroupedReservations', function (reservation) {
        ko.mapping.fromJS(reservation, GroupedReservations);
    });
**return self;**
    }

这篇关于Knockout.js不想绑定我的按位置分组的记录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆