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

时间:2017-02-18 22:50:20

标签: javascript listview nativescript webrequest

我正在使用NativeScript + JavaScript。我正在尝试使用API​​中的数据JSON-Data填充ListView。 Web-Request的工作方式应该如此,但我无法将从服务器获取的数据添加到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;

这是我的控制器的代码

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>

1 个答案:

答案 0 :(得分:0)

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

只需将当前内容保存在 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"));
        }
    });
};