本文介绍了Backbone.js + 休息.fetch() 后不填充集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Backbone 的新手.所以我正在尝试从 REST 服务中获取数据.

I'm new in Backbone. So I'm trying to fetch data from REST service.

这是我的简单代码:

$(function () {

    var Entity = Backbone.Model.extend({
        url: function() {
            return 'http://localhost:8080/rest/entity/'+this.id;
        }
    });

    var EntityList = Backbone.Collection.extend({
        model: Entity,
        url: 'http://localhost:8080/rest/entity'
    });

    var entityList = new EntityList();

    entityList.fetch();

});

我的休息服务返回下一个 JSON:

my rest service returns next JSON:

[{"id":1387,
  "version":3,
  "entityName":"entity01",
  "entityLabel":"Entity01",
  "entityPluralLabel":"Entity01",
  "attributes":
     [{"id":1425,
       "slot":"D001",
       "version":0,
       "attributeName":"dfield",
       "attributeType":
          {"id":7,
           "description":"Date",
           "attributeType":"date",
           "databaseType":"DATE"
          },
       "options":[],
       "order":2,
       "attributeLabel":"dField",
       "checked":null
      },
      {"id":1424,
       "slot":"S001",
       "version":0,
       "attributeName":"txfield",
       "attributeType":
          {"id":1,
           "description":"Textbox",
           "attributeType":"textbox",
           "databaseType":"STRING"
          },
       "options":[],
       "order":1,
       "attributeLabel":"txField",
       "checked":null
      }
     ]
 },
 {"id":1426,
  "version":3,
  "entityName":"entity02",
  "entityLabel":"Entity02",
  "entityPluralLabel":"Entity02",
  "attributes":
     [{"id":1464,
       "slot":"D001",
       "version":0,
       "attributeName":"dfield",
       "attributeType":
          {"id":7,
           "description":"Date",
           "attributeType":"date",
           "databaseType":"DATE"
          },
       "options":[],
       "order":2,
       "attributeLabel":"dField",
       "checked":null
      }
     ]
 }
]

在调试器中,我看到请求已发送到 REST 服务并收到响应,如何查看 entityList 集合是否填充了收到的数据?在调试器中 entityList.models 在 entityList.fetch() 之后为空;

In debugger I see that request was sent to REST service and response was recieved, how can I see if entityList collection is populated with recieved data or not? In debugger entityList.models is empty after entityList.fetch();

我的方法正确还是我的代码有问题?

Am I on right way or somthing is wrong with my code?

推荐答案

我认为您走对了路.但是因为 Backbone.Collection.fetch() 是异步的,你应该检查 entityList.models 的值不是在方法调用之后,而是在 success> fetch 回调.

I think you are on the right way. But because Backbone.Collection.fetch() is async, you should check value of entityList.models not right after the method call, but in success callback of fetch.

也就是说,这段代码会说模型列表是空的:

That is, this code will say that models list is empty:

entityList.fetch();
console.log(entityList.models); // => 0 (collection being fetched)

虽然此代码将在填充后打印集合中的模型数:

while this code will print number of models in the collection when it have been populated:

entityList.fetch({success: function(){
    console.log(entityList.models); // => 2 (collection have been populated)
}});

这篇关于Backbone.js + 休息.fetch() 后不填充集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:56