简体   繁体   English

Backbone.js集合get不起作用

[英]Backbone.js collection get does not work

var sgt1 = Backbone.Collection.extend({
        model: sgt2
    });

var a = new sgt1();
a.add({attr1: 'asd'});

Ok, up to now it is works but: 好的,到目前为止它是有效的但是:

a.get(0) returns undefined a.get(0)返回undefined

and

a.at(0) returns an obj a.at(0)返回一个obj

Why does get return undefined ? 为什么get回报undefined

With get() you query the collection by the models id field and not by its index like with at() . 使用get()您可以通过模型id字段查询集合,而不是使用at()查询其索引。 Please refer to the documentation . 请参阅文档

a.get(0) fetches by ID. a.get(0)按ID获取。 It is not the same as at which fetches by index position. 它是不一样的at其通过索引位置取出。 Since your model does not yet have an ID, get would not work. 由于你的模型还没有一个ID, get是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM