返回一个骨干收集和通过迭代 - 有没有查看 [英] Returning a Backbone Collection and iterating through - not with a View
问题描述
我返回对象的集合,从MongoDB数据库恢复,并希望找到多少款被退回,并能够通过他们迭代并创建基于某些DOM元素。我不使用Backbone.View并且还将使用Famo.us。
I am returning a collection of objects returned from a Mongodb database and want to find how many models were returned and be able to iterate through them and create some DOM elements based on that. I am not using a Backbone.View and will be using Famo.us.
我创建一个集合,并把它返回,但我似乎无法访问内部或他们的数据模型 - 知道你是否能帮助:)
I create a collection and get it returned BUT I can't seem to access the models inside or their data - wondered if you could help :)
下面是型号/系列:
define( function (require, exports, module) {
'use strict';
var Backbone = require('backbone');
var Book = Backbone.Model.extend({
idAttribute: "_id"
});
var BookCollection = Backbone.Collection.extend({
model: Book,
url: "http://localhost:3000/api/books"
});
module.exports = BookCollection;
});
在主文件我用实例化集合:
In the main file I Instantiate the Collection using:
this._bookCollection = new BookCollection();
this._bookCollection.fetch({reset: true});
var _bookCollection = this._bookCollection;
console.log(_bookCollection);
console.log(_bookCollection.length);
在'的console.log(_bookCollection)'将返回类似于:
The 'console.log(_bookCollection)' will return something similar to:
n {length: 0, models: Array[0], _byId: Object, constructor: function, model: function…}
_byId: Object
length: 250
models: Array[250]
[0 … 99]
[100 … 199]
[200 … 249]
length: 250
__proto__: Array[0]
__proto__: s
所以我知道它返回的东西,因为我期待250模型被退回。由于它不是一个真正的数组 - 你不能做_bookCollection.length(当然它不工作对我来说)。我怎样才能返回什么,所以我可以得到每个模型迭代,然后'做'与模型的东西吗?我使用Famo.us所以想创建一个比其他Backbone.View东西。
So I know it returns something as I am expecting 250 'models' to be returned. As its not really an array - you can't do _bookCollection.length (well it doesn't work for me). How can I iterate through what is returned so I can get each model and then 'do' something with that model? I am using Famo.us so want to create something other than a Backbone.View.
感谢
- 更新
在this._bookCollection.fetch({补充:真});该浏览器显示我有:
after "this._bookCollection.fetch({add:true});" the browser shows I have :
{length: 0, models: Array[0], _byId: Object, _events: Object, constructor: function…}_byId: Object_events: Objectlength: 250models: Array[250]__proto__: n
如果你展开它,它会告诉你的长度为250个,并在模型(数组[250])有我要找的资料,分成[00..99],[100..199]等等。
If you expand it, it tells you the length is 250 and in 'models' (Array[250]) there is the data I am looking for, split into [00..99], [100..199] etc.
我是检索收集错误或不能真做没有创建视图?
Am I retrieving the collection incorrectly or can't I really do it without creating a view?
推荐答案
Collection.fetch
是异步的,你的数据不会立即可用。你将不得不使用由返回要么承诺获取
:
Collection.fetch
is asynchronous, your data won't be immediately available. You will have to use either the promise returned by fetch
:
_bookCollection.fetch({reset: true}).then(function() {
console.log(_bookCollection.length, _bookCollection.pluck('_id'));
});
或骨干提供的事件:
_bookCollection.on("sync", function() {
console.log(_bookCollection.length, _bookCollection.pluck('_id'));
});
_bookCollection.fetch({reset: true});
一旦你的回调设置,您可以使用渲染适合你的任何手段。
Once your callbacks are set up, you can use whatever means of rendering suits you.
奖励:为什么绊倒的console.log 的你
这篇关于返回一个骨干收集和通过迭代 - 有没有查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!