0

このビューモデルを使用して、ユーザーとそのソーシャルグラフのリストを WCF サービスから読み込みます。ユーザーは正しく表示されますが、socialgraph エントリは表示されません。サービスと返されたjsonを確認しましたが、すべて問題ないようです。

モデルを別の sth に変更する必要がありますか、それとも ViewModel に物をロードする方法ですか? ありがとう

$(document).ready(function () {

var viewModel = {

    users: ko.observableArray([]),              

    loadUsers: function () {
        OData.read("Service_UserProfile/", function (data) {

            viewModel.users.removeAll();
            $.each(data.results, function (index, item) {                    
                var socialgraphs = viewModel.loadSocialGraph();
                var user = new UserProfileModel(item, socialgraphs);                   
                viewModel.users.push(user);                   
            });
        });
    },

    loadSocialGraph: function () {

        var result = new Array();
        // user id will be loaded dynamically in later steps
        OData.read("/Service_UserProfile(1)/Socialgraph/", function (data) {

            $.each(data.results, function (index, item) {                   
                result.push(new SocialGraph(item));                    
           });
        });        
        return result;
    }
};

ko.applyBindings(viewModel);   
viewModel.loadUsers();

});

モデル

function UserProfileModel(item,socialgraphs) {
    this.Id = ko.observable(item.Id),
    this.Nickname = ko.observable(item.Nickname),
    this.socialgraphs = ko.observableArray(socialgraphs)
};

function SocialGraph(item) {
    this.Id = ko.observable(item.Id),
    this.StartTime = ko.observable(item.StartTime),
    this.Latitude = ko.observable(item.Latitude),
    this.Longitude = ko.observable(item.Longitude)
};

景色

<table>
    <thead>
        <tr>
            <th>User ID</th>
            <th>Nickname
            </th>
            <th>Social Graph
            </th>
        </tr>
    </thead>
    <tbody data-bind="foreach: users">
        <tr>
            <td data-bind="text: Id"></td>
            <td data-bind="text: Nickname"></td>
            <td>
                <ul data-bind="foreach: socialgraphs">
                    <li data-bind="text: Id"></li>
                    <li data-bind="dateString: StartTime"></li>
                    <li data-bind="text: Latitude"></li>
                    <li data-bind="text: Longitude"></li>
                </ul>
            </td>
        </tr>

    </tbody>

</table>
4

1 に答える 1

0

変更する必要があります:

loadSocialGraph: function () {

    var result = ko.observableArray();
    // user id will be loaded dynamically in later steps
    OData.read("/Service_UserProfile(1)/Socialgraph/", function (data) {

        $.each(data.results, function (index, item) {                   
            result.push(new SocialGraph(item));                    
       });
    });        
    return result;
}

function UserProfileModel(item,socialgraphs) {
    this.Id = ko.observable(item.Id),
    this.Nickname = ko.observable(item.Nickname),
    this.socialgraphs = socialgraphs
};

理由:右側
の行this.socialgraphs = ko.observableArray(socialgraphs)
socialgraphs で [] です。一定の時間間隔が経過した後にのみ、値が入力されます。また、観測可能な配列ではないため、ノックアウトはいくつかのアイテムがプッシュされたことに気づきません。つまり、空のままになります。

于 2013-04-25T13:55:12.607 に答える