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

    • <bdo id='5si8h'></bdo><ul id='5si8h'></ul>

      <small id='5si8h'></small><noframes id='5si8h'>

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

      Backbone.js:模型没有出现

      时间:2024-04-19

          <tbody id='7aEIY'></tbody>
            <bdo id='7aEIY'></bdo><ul id='7aEIY'></ul>
          • <tfoot id='7aEIY'></tfoot>

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

              <small id='7aEIY'></small><noframes id='7aEIY'>

              1. 本文介绍了Backbone.js:模型没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想显示一个简单的语言列表.

                I want to display a simple list of languages.

                class Language extends Backbone.Model
                
                    defaults:
                        id: 1
                        language: 'N/A'
                
                class LanguageList extends Backbone.Collection
                
                    model: Language
                    url: '/languages'
                
                languages = new LanguageList
                
                class LanguageListView extends Backbone.View
                
                    el: $ '#here'
                
                    initialize: ->
                        _.bindAll @
                        @render()
                
                    render: ->
                        languages.fetch()
                        console.log languages.models
                
                list_view = new LanguageListView
                

                languages.models 显示为空,尽管我检查了请求是否已进入并已获取语言.我错过了什么吗?

                languages.models appears empty, although I checked that the request came in and languages were fetched. Am I missing something?

                谢谢.

                推荐答案

                fetch 调用是异步的:

                The fetch call is asynchronous:

                fetch collection.fetch([options])

                从服务器获取此集合的默认模型集,当它们到达时重置集合.选项哈希采用 successerror 回调,它们将作为参数传递 (collection, response).当模型数据从服务器返回时,集合将重置.

                Fetch the default set of models for this collection from the server, resetting the collection when they arrive. The options hash takes success and error callbacks which will be passed (collection, response) as arguments. When the model data returns from the server, the collection will reset.

                结果是您的 console.log 语言.modelslanguages.fetch() 调用从服务器返回任何内容之前被调用.

                The result is that your console.log languages.models is getting called before the languages.fetch() call has gotten anything back from the server.

                所以你的 render 应该看起来更像这样:

                So your render should look more like this:

                render: ->
                    languages.fetch
                        success: -> console.log languages.models
                    @ # Render should always return @
                

                这应该会让你在控制台上得到一些东西.

                That should get you something on the console.

                initialize 中调用 languages.fetch 并将 @render 绑定到集合的 reset 事件;然后你可以在集合准备好时把东西放在页面上.

                It would make more sense to call languages.fetch in initialize and bind @render to the collection's reset event; then you could put things on the page when the collection is ready.

                此外,CoffeeScript 很少需要 _.bindAll @.您应该使用 => 创建相关方法.

                Also, _.bindAll @ is rarely needed with CoffeeScript. You should create the relevant methods with => instead.

                这篇关于Backbone.js:模型没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:是否有必要在递归调用的计时器内清除Timeout? 下一篇:包含承诺的for循环中的词法范围?

                相关文章

                  <bdo id='UjJlX'></bdo><ul id='UjJlX'></ul>
                <legend id='UjJlX'><style id='UjJlX'><dir id='UjJlX'><q id='UjJlX'></q></dir></style></legend>

                  1. <small id='UjJlX'></small><noframes id='UjJlX'>

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