-1

告诉我,我做错了什么。在输出我得到 3 个空行

我对我在做什么的建议从这些数据中得出错误的结论

这是一个代码示例

main.js

var observable = require("data/observable");
var virtualArrayModule = require("data/virtual-array");
var http = require("http");

var mainViewModel = new observable.Observable();
var array = new virtualArrayModule.VirtualArray(3);

mainViewModel.tapAction = function () {

array.on(virtualArrayModule.knownEvents.itemsLoading, function (args) {
    http.getJSON("http://www.reddit.com/r/news.json?limit=" + args.count).then(function (r) {

        var itemsToLoad = r.data.children.map(function (i) {
            return i.data.title;
        });

        array.load(args.index, itemsToLoad);

    }, function (e) {
        done(e);
    });

});

mainViewModel.set("redditItems", array);

};

exports.mainViewModel = mainViewModel;

和 main.xml

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
<StackLayout>
<Button text="Загрузить" tap="{{ tapAction }}" />
<ListView items="{{ redditItems }}">
    <ListView.itemTemplate>
      <GridLayout columns="auto, *, auto" rows="auto, 25">
        <Label text="{{ title }}" textWrap="true" col="1" colSpan="2" minHeight="50" />
      </GridLayout>
    </ListView.itemTemplate>
</ListView>
<Label text="{{ error }}" cssClass="message"/>
</StackLayout>
</Page>
4

1 回答 1

0

In your main.xml you want to show a property "title" for every item in the redditItems as the text of the label.

<ListView items="{{ redditItems }}">
  <ListView.itemTemplate>
    <GridLayout columns="auto, *, auto" rows="auto, 25">
      <Label text="{{ title }}" textWrap="true" col="1" colSpan="2" minHeight="50" />
    </GridLayout>
  </ListView.itemTemplate>
</ListView>

But in the itemsLoading event of your ListView, you are only adding a string to the redditItems array.

var itemsToLoad = r.data.children.map(function (i) {
  return i.data.title;
});
array.load(args.index, itemsToLoad);

The view has nothing to show because there is not a "title" property present in the items in the redditItems array. Change the code above to the following to see if it works (haven't tested it myself).

var itemsToLoad = r.data.children.map(function (i) {
  return i.data;
});
array.load(args.index, itemsToLoad);

Now the "title" property should be available on every item in your redditItems array and the view can display the value of this property.

于 2015-04-20T21:48:02.120 回答