gpt4 book ai didi

javascript - 如何使用来自 WebService 的 JSON 数据填充 NativeScript 中的 ListView?

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:31 26 4
gpt4 key购买 nike

我正在使用 NativeScript + JavaScript。我正在尝试使用 API 中的数据 JSON 数据填充 ListView。 Web 请求按其应有的方式工作,但我无法设法将从服务器获取的数据添加到 ListView 中。但我可以将硬编码数据添加到 ListView 中。

这是我的 ViewModel 的代码:

"use strict";
var config = require ("../../shared/config");
var observableModule = require("data/observable");
var observable_array_1 = require("data/observable-array");
var arrayOfDataItems;

var ViewModel = (function () {

function ViewModel() {
this.initDataItems();
}
Object.defineProperty(ViewModel.prototype, "dataItems", {
get: function () {
return this._items;
},
enumerable: true,
configurable: true
});
ViewModel.prototype.onAddItemClick = function (args) {
this._items.push(new DataItem("1","Fooo","Bar"));
};

ViewModel.prototype.initDataItems = function (args) {
this._items = new observable_array_1.ObservableArray();

//conifg.apiUrl holds the URL where i make the GET Request
//I can populate the ListView if i write this._items.push(new DataItem("1","Lorem","Ipsum" here
return fetch(config.apiUrl + "competition")
.then(function(response) {
return response.json();
}).then(function(data) {
for (var i = 0; i < data.length; i++) {
//I can't populate the ListView here when I get the response from the server
this._items.push(new DataItem(data[i].id,data[i].name,"Useless Description"));
}
});
};

return ViewModel;
}());
exports.ViewModel = ViewModel;

var DataItem = (function (_super) {
__extends(DataItem, _super);
function DataItem(id, name, description) {
_super.call(this);
this.id = id;
this.itemName = name;
this.itemDescription = description;
}
Object.defineProperty(DataItem.prototype, "id", {
get: function () {
return this.get("_id");
},
set: function (value) {
this.set("_id", value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(DataItem.prototype, "itemName", {
get: function () {
return this.get("_itemName");
},
set: function (value) {
this.set("_itemName", value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(DataItem.prototype, "itemDescription", {
get: function () {
return this.get("_itemDescription");
},
set: function (value) {
this.set("_itemDescription", value);
},
enumerable: true,
configurable: true
});
return DataItem;
}(observableModule.Observable));
exports.DataItem = DataItem;

这是我的 Controller 的代码

var BasePage = require("../../shared/BasePage");
var topmost = require("ui/frame").topmost;




var CompetitionPage = function() {};
CompetitionPage.prototype = new BasePage();
CompetitionPage.prototype.constructor = CompetitionPage;

var viewModel = require("~/views/results/observable-array-model");

CompetitionPage.prototype.onPageLoaded = function(args) {
var page = args.object;
page.bindingContext = new viewModel.ViewModel
}

module.exports = new CompetitionPage();

这是我的 ListView 的 XML

      <GridLayout loaded="onPageLoaded" orientation="vertical" rows="50, *">
<StackLayout orientation="vertical" backgroundColor="#f8f8f8">
<StackLayout row="0" orientation="horizontal" horizontalAlignment="center">
<Button text="Add" tap="{{onAddItemClick}}" ios:margin="0"/>
<Button text="Del" tap="{{onRemoveItemClick}}" ios:margin="10"/>
<Button text="Update" tap="{{onUpdateItemClick}}" ios:margin="10"/>
<Button text="Reset" tap="{{onResetClick}}" ios:margin="10"/>
</StackLayout>
</StackLayout>
<lv:RadListView items="{{ dataItems }}" row="1" id="ls">
<lv:RadListView.listViewLayout>
<lv:ListViewLinearLayout scrollDirection="Vertical"/>
</lv:RadListView.listViewLayout>
<lv:RadListView.itemTemplate>
<StackLayout orientation="vertical">
<Label fontSize="20" text="{{ _itemName }}"/>
<Label fontSize="14" text="{{ _itemDescription }}"/>
<Label fontSize="10" text="{{ _id }}" />
</StackLayout>
</lv:RadListView.itemTemplate>
</lv:RadListView>

</GridLayout>

最佳答案

看起来您没有将正确this传递给获取操作的.then

只需将当前的 this 保存在 WeakRef 中并在回调中使用它:

ViewModel.prototype.initDataItems = function (args) {
this._items = new observable_array_1.ObservableArray();

var that = new WeakRef(this);

return fetch(config.apiUrl + "competition")
.then(function(response) {
return response.json();
}).then(function(data) {
for (var i = 0; i < data.length; i++) {

that.get()._items.push(new DataItem(data[i].id,data[i].name,"Useless Description"));
}
});
};

关于javascript - 如何使用来自 WebService 的 JSON 数据填充 NativeScript 中的 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com