1

在 Titanium 上,我有以下文件:

控制器/tab_matches.js

var matches = Alloy.Collections.match;
matches.fetch();

console.log(matches);

模型/match.js

var Alloy = require("alloy");
exports.definition = {
  config : {
    "URL" : "http://" + Alloy.CFG.server.ip + ":" + Alloy.CFG.server.port + "/api/matches/",
    "debug" : Alloy.CFG.debug,
    "adapter" : {
      "type" : "restapi",
      "collection_name" : "matches",
      "idAttribute" : "id"
    }
  },
  extendModel : function(Model) {
    _.extend(Model.prototype, {});
    return Model;
  },
  extendCollection : function(Collection) {
    _.extend(Collection.prototype, {});
    return Collection;
  }
}

意见/tab_matches.xml

<Alloy>
  <Collection src="match" />
  <Window title="Matches">
    <ListView id="list_matches">
  </Window>
</Alloy>

对于 REST API,我使用这个:https ://github.com/viezel/napp.alloy.adapter.restapi

为什么我的集合在 fetch 调用后是空的?无法循环进入匹配项,matches.length 为 0。

这是日志,正如我们所见,远程服务器返回了一个元素。

[INFO] :   [REST API] options:
[INFO] :   {
[INFO] :       error = "<KrollCallback: 0xad60b20>";
[INFO] :       headers =     {
[INFO] :           "Content-Type" = "application/json";
[INFO] :       };
[INFO] :       parse = 1;
[INFO] :       success = "<KrollCallback: 0xad60ac0>";
[INFO] :       type = GET;
[INFO] :       url = "http://192.168.1.10:3000/api/matches/";
[INFO] :   }
[INFO] :   {
[INFO] :       "_byCid" =     {
[INFO] :       };
[INFO] :       "_byId" =     {
[INFO] :       };
[INFO] :       "_onModelEvent" = "<KrollCallback: 0x138742d0>";
[INFO] :       "_prepareModel" = "<KrollCallback: 0x1388f140>";
[INFO] :       "_removeReference" = "<KrollCallback: 0x1388c220>";
[INFO] :       "_reset" = "<KrollCallback: 0x1387c9f0>";
[INFO] :       add = "<KrollCallback: 0x1386bcd0>";
[INFO] :       all = "<KrollCallback: 0x1386cef0>";
[INFO] :       any = "<KrollCallback: 0x1386d900>";
[INFO] :       at = "<KrollCallback: 0x13882900>";
[INFO] :       bind = "<KrollCallback: 0x13881310>";
[INFO] :       chain = "<KrollCallback: 0x13877890>";
[INFO] :       config =     {
[INFO] :           Model = "<KrollCallback: 0x13876d80>";
[INFO] :           URL = "http://192.168.1.10:3000/api/matches/";
[INFO] :           adapter =         {
[INFO] :               "collection_name" = matches;
[INFO] :               idAttribute = id;
[INFO] :               type = restapi;
[INFO] :           };
[INFO] :           debug = 1;
[INFO] :       };
[INFO] :       constructor = "<KrollCallback: 0x1387f0d0>";
[INFO] :       contains = "<KrollCallback: 0x138803d0>";
[INFO] :       create = "<KrollCallback: 0x13873f50>";
[INFO] :       detect = "<KrollCallback: 0x1388d530>";
[INFO] :       each = "<KrollCallback: 0x1386c070>";
[INFO] :       every = "<KrollCallback: 0x13884b30>";
[INFO] :       fetch = "<KrollCallback: 0x1386c140>";
[INFO] :       filter = "<KrollCallback: 0x13883d80>";
[INFO] :       find = "<KrollCallback: 0x138815f0>";
[INFO] :       first = "<KrollCallback: 0x1387f5a0>";
[INFO] :       forEach = "<KrollCallback: 0x13887180>";
[INFO] :       get = "<KrollCallback: 0x1387e690>";
[INFO] :       getByCid = "<KrollCallback: 0x13881290>";
[INFO] :       groupBy = "<KrollCallback: 0x13884fa0>";
[INFO] :       idAttribute = id;
[INFO] :       include = "<KrollCallback: 0x13882090>";
[INFO] :       indexOf = "<KrollCallback: 0x13883840>";
[INFO] :       initial = "<KrollCallback: 0x13888180>";
[INFO] :       initialize = "<KrollCallback: 0x1385a300>";
[INFO] :       invoke = "<KrollCallback: 0x138803f0>";
[INFO] :       isEmpty = "<KrollCallback: 0x138844b0>";
[INFO] :       last = "<KrollCallback: 0x13870460>";
[INFO] :       lastIndexOf = "<KrollCallback: 0x13884c30>";
[INFO] :       length = 0;
[INFO] :       map = "<KrollCallback: 0x13887ca0>";
[INFO] :       max = "<KrollCallback: 0x13884cf0>";
[INFO] :       min = "<KrollCallback: 0x13884d50>";
[INFO] :       model = "<KrollCallback: 0x13858ad0>";
[INFO] :       models =     (
[INFO] :       );
[INFO] :       off = "<KrollCallback: 0x13887100>";
[INFO] :       on = "<KrollCallback: 0x138870e0>";
[INFO] :       parse = "<KrollCallback: 0x138754c0>";
[INFO] :       pluck = "<KrollCallback: 0x13886c00>";
[INFO] :       pop = "<KrollCallback: 0x1386dcc0>";
[INFO] :       push = "<KrollCallback: 0x1386dca0>";
[INFO] :       reduce = "<KrollCallback: 0x1388d110>";
[INFO] :       reduceRight = "<KrollCallback: 0x1388d510>";
[INFO] :       reject = "<KrollCallback: 0x13884b10>";
[INFO] :       remove = "<KrollCallback: 0x1386bcf0>";
[INFO] :       reset = "<KrollCallback: 0x1388f120>";
[INFO] :       rest = "<KrollCallback: 0x138881e0>";
[INFO] :       select = "<KrollCallback: 0x13883de0>";
[INFO] :       shift = "<KrollCallback: 0x1387e670>";
[INFO] :       shuffle = "<KrollCallback: 0x138838c0>";
[INFO] :       size = "<KrollCallback: 0x1386d9d0>";
[INFO] :       some = "<KrollCallback: 0x1386cf50>";
[INFO] :       sort = "<KrollCallback: 0x13886be0>";
[INFO] :       sortBy = "<KrollCallback: 0x1388d480>";
[INFO] :       sortedIndex = "<KrollCallback: 0x13886210>";
[INFO] :       sync = "<KrollCallback: 0x13859d80>";
[INFO] :       toArray = "<KrollCallback: 0x1386d9b0>";
[INFO] :       toJSON = "<KrollCallback: 0x1385a320>";
[INFO] :       trigger = "<KrollCallback: 0x138874d0>";
[INFO] :       unbind = "<KrollCallback: 0x1388c2c0>";
[INFO] :       unshift = "<KrollCallback: 0x1386ccc0>";
[INFO] :       where = "<KrollCallback: 0x13882920>";
[INFO] :       without = "<KrollCallback: 0x13876b20>";
[INFO] :   }
[INFO] :   [REST API] server read response:
[INFO] :   (
[INFO] :           {
[INFO] :           id = 3;
[INFO] :           lastminute = 0;
[INFO] :           opengroup = 1;
[INFO] :       }
[INFO] :   )
4

1 回答 1

3

我通过执行以下代码解决了这个问题。

fetch 是一个异步调用,所以调用后集合是空的。结果必须在成功回调中处理。

matches.fetch({
    success: function(){
        console.log(matches.models);
        //parse to listView
        var data = [];
        _.each(matches.models, function(element, index, list){
            data.push(
                Alloy.createController("ListItem", {
                    title : element.attributes['title'] //whatever you need to set
                }).getView()
            );
        });
        // add to your listview
    },
    error: function(){
        // something is wrong.. 
    }
});

感谢 Mads Moller:https ://developer.appcelerator.com/question/153642/empty-collection-after-fetch

于 2013-06-16T11:51:39.230 回答