异步功能中可观察的设定值

编程入门 行业动态 更新时间:2024-10-13 08:25:34
本文介绍了异步功能中可观察的设定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用DevExtreme和淘汰赛.我想从服务器获取JSON-Data并将其保存到可观察对象中.

I'm using DevExtreme and knockout. I want to fetch JSON-Data from a server and save it into an observable.

当前方法:

var dataArray = ko.observableArray(); var dataId = ko.observable(""); MyApp.overview = function (params) { "use strict"; var viewModel = { [...] } return viewModel; }; function getDataFromJson() { $.ajax({ url: 'localhost:56253/test/3?format=json', dataType: 'json', success: function (data) { var entries = $.map(data, function (item) { return new entry(item) }); // first entry is ID for (var i = 1; i < entries.length; i++) { dataArray.push(entries[i]); } } }); } function getIDFromJson() { $.ajax({ url: 'localhost:56253/test/3?format=json', dataType: 'json', success: function (data) { dataId(data.ID); } }); } function entry(data) { this.A = data.A, this.B = data.B, this.C = data.C }

我是这些情况的新手,所以不确定自己的方法.为了进行测试,我调用了通过按钮手动获取JSON-Data的函数,并且获得了所需的数据,但是可观察对象都包含以下内容:

I'm new to these scenarios, so I'm not sure if my apporach. For testing, I'm calling the functions to get the JSON-Data manually via buttons, and I get the needed data, but the observables both contain this:

function c(){if(0<arguments.length)return c.tb(c[E],arguments[0])&&(c.ga(),c[E]=arguments[0],c.fa()),this;a.l.oc(c);return c[E]}

我想念什么?还是这种方法通常是不好的做法?

What am I missing? Or is this approach bad practice in general?

推荐答案

如何使用淘汰表映射插件和fromJSON命令(根据数据格式,您可能需要使用fromJS)会映射来自源的数据.

How I do it is by using the Knockout Mapping Plugin and the fromJSON command (you might need to use fromJS depending on your data format) this maps the data as it comes from the source.

function getDataFromJson() { $.ajax({ url: 'localhost:56253/test/3?format=json', dataType: 'json', success: function (data) { ko.mapping.fromJSON(data, {}, self.dataArray); } }); }

更多推荐

异步功能中可观察的设定值

本文发布于:2023-11-13 02:22:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设定值   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!