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

标签 javascript listview nativescript webrequest

我正在使用 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/

相关文章:

javascript - 需要添加功能以确保在提交多页表单之前至少选中 1 个复选框?

android ListView item点击闪烁bug

带有微调器和复选框的 Android Listview

nativescript - 了解页面事件

react-native - 混合 JavaScript 移动开发 - Apache Cordova vs Capacitor with Ionic vs NativeScript vs React Native

javascript - Safari 嵌套滚动条错误?

javascript - 将 undefined 强制转换为 0

javascript - 在 JS 数组中查找特定元素

android - 使用 ActionBarSherlock 看不到我的 fragment

NativeScript Vue 发送带有表单数据的请求(multipart/form-data)