为什么Backbone.js的访问模型时返回一个空数组? [英] Why is backbone.js returning an empty array when accessing models?

查看:99
本文介绍了为什么Backbone.js的访问模型时返回一个空数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问其收藏的路由器。我的循环不是通过模型迭代,所以我尝试登录的收集,看看它返回。原来,当我登录的收集直接我看到所有型号的预期。但是,如果我尝试登录该集合的模型属性我得到一个空数组!它没有意义。这些线直接彼此跟随。我试图改变秩序,得到了相同的结果。

 的console.log(this.collection);
= GT;镜头
    _byCid:对象
    _byId:对象
    长度:15
    型号:数组[15]
    __proto__:射击
    ...的console.log(this.collection.models);
= GT; []的console.log(this.collection.length);
= GT; 0

为什么会出现这种情况?

下面是code,因为它是在路由器给这个何处code被烧成一个更好的环境:

 #路由器
类Draft.Routers.Shots扩展Backbone.Router
  路线:
    '':'指数'
    '张/:身份证':'秀'  初始化: - >
    @collection =新Draft.Collections.Shots()
    @ collection.fetch()  索引: - >
    的console.log @collection
    @的console.log collection.models


解决方案

吉姆,

这不能解决您的问题 - 你已经工作了这一点。但它解释了为什么你看到控制台输出你看。

当您运行的console.log(本),你的输出对象本身和控制台链接引用的(三分球,如果你喜欢),以内部变量。

当你在控制台中看着它,在当时的console.log(本)的运行模型区是空的,但在当时,你的在日志中,收集已完成加载模型和内部数组变量被更新,而引用的到对象日志变量显示当前内容。

基本上在执行console.log(本),内部模型变量继续其正常生活和控制台显示在你看着它,而不是在你把它称为时止的当前状态。
随着执行console.log(this.models),阵列倾倒原样,没有参考保持所有的内在价值是由一个倾倒的..

这行为是相当简单再现了短超时,看到这个小提琴.. http://jsfiddle.net/bendog/XVkHW /

I have a router accessing its collection. My for loop wasn't iterating through the models so I tried logging the collection to see what it returned. Turns out when I log the collection directly I see all of the models as expected. But if I try to log the models attribute of the collection I get an empty array! It doesn't make sense. These lines are directly following each other. I tried changing the order and got the same outcome.

console.log(this.collection);
=> Shots
    _byCid:    Object
    _byId:     Object
    length:    15
    models:    Array[15]
    __proto__: Shots
    ...

console.log(this.collection.models);
=> []

console.log(this.collection.length);
=> 0

Why would this happen?

Here is the code as it is in the router to give a better context of where this code is firing:

# Routers
class Draft.Routers.Shots extends Backbone.Router
  routes:
    ''            : 'index'
    'shots/:id'   : 'show'

  initialize: ->
    @collection = new Draft.Collections.Shots()
    @collection.fetch()

  index: ->
    console.log @collection
    console.log @collection.models

解决方案

Jim,

This doesn't fix your problem - you've worked that out. But it explains why you're seeing the console output you see.

When you run console.log(this), you output the object itself and the console links references (pointers if you like) to the inner variables.

When you're looking at it in the console, at the time the console.log(this) runs the models area is empty, but at the time you look at the logs, the collection has finished loading the models and the inner array variable is updated, AND the reference to that variable in the object log shows the current content.

Basically in console.log(this),inner models variable continues its normal life and the console shows the current status at the time you're looking at it, not at the time you called it. With console.log(this.models), the array is dumped as is, no reference is kept and all the inner values are dumped one by one..

That behaviour is quite simple to reproduce with a short timeout, see this fiddle.. http://jsfiddle.net/bendog/XVkHW/

这篇关于为什么Backbone.js的访问模型时返回一个空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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