使用arrayMap函数淘汰可观察对象的observablearray [英] Knockout observablearray of observables with arrayMap function

查看:94
本文介绍了使用arrayMap函数淘汰可观察对象的observablearray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建可观察变量的可观察数组时遇到问题.我在Google上搜索,但未找到解决方案.我可能没有注意到这很简单,因为我是Knockout的新手. 但是我有模型:

I have a problem creating observable array of observables. I search on google but didn't find a solution. It may be something simple that I can't notice, because I'm new to Knockout. But I have the model:

eventsModel = function () {
   var self = this;

   self.endTimeInMinutes = ko.observable();
   self.events = ko.observableArray([]);
   self.startTripTime = ko.observable();
   self.endTripTime = ko.observable();
}

并且我想在数组中包含一个可观察项,因此我编写了一个ViewModel并绑定了模型.

and I want to have an observables items in my array so I write a ViewModel and bind the model.

eventItemViewModel = function(o) {
    var self = this;

    self.BeginInMinutes = ko.observable(o.BeginInMinutes);
    self.Type = ko.observable(o.Type);
};

var events = new eventsModel();
ko.applyBindings(events);

我正在使用AJAX获取数据:

And I'm fetching the data using AJAX:

function GetEvents() {
    $.ajax({
        url: "Contact.aspx/GetEvents",
        async: true,
        type: "POST",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            var temp = data.d;
            endTimeInMinutes = temp["EndInMinutes"];
            eventsArr = temp["Events"];
            eventsArray = JSON.parse(JSON.stringify(eventsArr));
            events.events(eventsArray);
        },
    });
}

在这之后,我有了一个可观察的数组,但是里面没有可观察的值.现在,我尝试添加我的AJAX方法:

After this I have an observable array but without observables values inside. Now I trying to add in my AJAX method:

events.events(ko.utils.arrayMap(eventsArray, function(eve) {return new eventItemViewModel(eve); }));

但是,如果我对此执行console.log,那么我得到的是对象数组,并且在每个对象中都有一个BeginInMinutes和Type,但其值类似于function d()... etc.

But if I do console.log on this, then I am getting array of objects, and in each object there is a BeginInMinutes and Type, but it's value is like function d()... etc.

我真的被它卡住了,我相信我在某个地方犯了一个非常简单的错误.

I'm really get stucked with it, and I believe I made some very simple mistake somewhere.

感谢您的帮助.

推荐答案

您已经拥有一个observableArray元素,其中包含observable元素,
您的问题实际上是获取这些值
使用此代码获取数组中第一个元素的实际值.

You already got an observableArray with observable element inside it,
Your problem really is with getting those values
use this code to get the real value of the first element in the array.

console.log(events.events()[0].BeginInMinutes());

这篇关于使用arrayMap函数淘汰可观察对象的observablearray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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