<bdo id='mz0e3'></bdo><ul id='mz0e3'></ul>
  • <i id='mz0e3'><tr id='mz0e3'><dt id='mz0e3'><q id='mz0e3'><span id='mz0e3'><b id='mz0e3'><form id='mz0e3'><ins id='mz0e3'></ins><ul id='mz0e3'></ul><sub id='mz0e3'></sub></form><legend id='mz0e3'></legend><bdo id='mz0e3'><pre id='mz0e3'><center id='mz0e3'></center></pre></bdo></b><th id='mz0e3'></th></span></q></dt></tr></i><div id='mz0e3'><tfoot id='mz0e3'></tfoot><dl id='mz0e3'><fieldset id='mz0e3'></fieldset></dl></div>

      1. <legend id='mz0e3'><style id='mz0e3'><dir id='mz0e3'><q id='mz0e3'></q></dir></style></legend>

        <small id='mz0e3'></small><noframes id='mz0e3'>

      2. <tfoot id='mz0e3'></tfoot>

        为什么backbone.js在访问模型时返回一个空数组?

        Why is backbone.js returning an empty array when accessing models?(为什么backbone.js在访问模型时返回一个空数组?)

          • <bdo id='F7n9r'></bdo><ul id='F7n9r'></ul>
              • <tfoot id='F7n9r'></tfoot>
                <i id='F7n9r'><tr id='F7n9r'><dt id='F7n9r'><q id='F7n9r'><span id='F7n9r'><b id='F7n9r'><form id='F7n9r'><ins id='F7n9r'></ins><ul id='F7n9r'></ul><sub id='F7n9r'></sub></form><legend id='F7n9r'></legend><bdo id='F7n9r'><pre id='F7n9r'><center id='F7n9r'></center></pre></bdo></b><th id='F7n9r'></th></span></q></dt></tr></i><div id='F7n9r'><tfoot id='F7n9r'></tfoot><dl id='F7n9r'><fieldset id='F7n9r'></fieldset></dl></div>

                  <tbody id='F7n9r'></tbody>
                <legend id='F7n9r'><style id='F7n9r'><dir id='F7n9r'><q id='F7n9r'></q></dir></style></legend>

                  <small id='F7n9r'></small><noframes id='F7n9r'>

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

                  问题描述

                  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在访问模型时返回一个空数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                  How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社
                  How to use coffeescript in developing web-sites?(如何在开发网站时使用coffeescript?)
                  <legend id='MQuT1'><style id='MQuT1'><dir id='MQuT1'><q id='MQuT1'></q></dir></style></legend>

                  <small id='MQuT1'></small><noframes id='MQuT1'>

                      <tbody id='MQuT1'></tbody>
                    <tfoot id='MQuT1'></tfoot>

                    1. <i id='MQuT1'><tr id='MQuT1'><dt id='MQuT1'><q id='MQuT1'><span id='MQuT1'><b id='MQuT1'><form id='MQuT1'><ins id='MQuT1'></ins><ul id='MQuT1'></ul><sub id='MQuT1'></sub></form><legend id='MQuT1'></legend><bdo id='MQuT1'><pre id='MQuT1'><center id='MQuT1'></center></pre></bdo></b><th id='MQuT1'></th></span></q></dt></tr></i><div id='MQuT1'><tfoot id='MQuT1'></tfoot><dl id='MQuT1'><fieldset id='MQuT1'></fieldset></dl></div>
                          • <bdo id='MQuT1'></bdo><ul id='MQuT1'></ul>