главная страница. xml
<Page loaded="pageLoaded" class="page" xmlns="http://www.nativescript.org/tns.xsd"
xmlns:lv="nativescript-ui-listview"
xmlns:Card="nativescript-cardview">
<ActionBar title="Home" class="action-bar">
</ActionBar>
<lv:RadListView id="ls" items="{{ dataItems }}" row="0" loadOnDemandMode="Auto"
loadMoreDataRequested="{{onLoadMoreItemsRequested}}">
<lv:RadListView.itemTemplate>
<Card:CardView class="card" elevation="40" radius="10" >
<StackLayout class="card-layout" orientation="horizontal" >
<StackLayout class="card-layout" orientation="vertical" >
<Label fontSize="20" text="{{ title }}" marginBottom="8" />
</StackLayout>
</StackLayout>
</Card:CardView>
</lv:RadListView.itemTemplate>
</lv:RadListView>
</Page>
главная страница. js
var HomeViewModel = require("./main-view-model");
var homeViewModel = new HomeViewModel();
function pageLoaded(args) {
var page = args.object;
page.bindingContext = homeViewModel;
listView = page.getViewById("ls");
homeViewModel.initDataItems();
homeViewModel.addMoreItemsFromSource(3);
}
exports.pageLoaded = pageLoaded;
main-view-model. js
const httpModule = require("tns-core-modules/http");
var observableModule = require("tns-core-modules/data/observable");
var ObservableArray = require("tns-core-modules/data/observable-array").ObservableArray;
var posts = require("./posts.json");
var url1="https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Janelle%20Mon%C3%A1e&prop=categories";
function HomeViewModel() {
var viewModel = observableModule.fromObject({
_sourceDataItems: [],
initDataItems: function () {
*//This function is for local json Its working as expected.
this._sourceDataItems = new ObservableArray();
for (let i = 0; i < posts.names.length; i++) {
this._sourceDataItems.push({ name: posts.names[i] });
}//*
httpModule.request({
url: url1,
method: "GET"
}).then((response) => {
for( var i=0; i<response.length;i++){
this. _sourceDataItems.push({title: response.query.pages.categorymembers[i].title})
}
}, (e) => {
});
},
dataItems: [],
addMoreItemsFromSource: function (chunkSize) {
let newItems = this._sourceDataItems.splice(0, chunkSize);
this.dataItems = this.dataItems.concat(newItems);
},
onLoadMoreItemsRequested: function (args) {
console.log("---load item called----");
const that = new WeakRef(this);
const listView = args.object;
if (this._sourceDataItems.length > 0) {
setTimeout(function () {
that.get().addMoreItemsFromSource(3);
listView.notifyLoadOnDemandFinished();
}, 4000);
args.returnValue = true;
} else {
args.returnValue = false;
listView.notifyLoadOnDemandFinished(false);
}
},
});
return viewModel;
}
module.exports = HomeViewModel;
В моем фрагменте кода я не могу получить данные из json api, но из локального json, получая данные. Если я поставлю json API-данные не отображаются и не выдают ошибку также. Пожалуйста, помогите мне получить данные из API, когда загрузите больше данных запроса. https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Janelle%20Mon%C3%A1e&prop=categories, это для демонстрации при написании используемой функции fetch (), httpmodule.get json () Это не работает для всех, дайте мне знать, где я допускаю ошибку.