Backbone.js的+休息。收藏获取后,不填充() [英] Backbone.js + Rest. Collection is not populated after fetch()

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

问题描述

我在新的骨干。所以我想从REST服务获取数据。

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

这是我简单的code:

this is my simple code:

$(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();

我是在正确的方式或财产以后不对我的code?

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

推荐答案

我认为你是在正确的道路。但由于 Backbone.Collection.fetch()是异步,你应该检查 entityList.models 后没有正确的价值方法调用,但在获取成功回调。

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.

也就是说,这个code会说,机型列表是空的:

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

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

而这code将打印已填充时,集合中的模型号:

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的+休息。收藏获取后,不填充()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆