Backbone.js:模型未出现

发布于 2025-01-04 21:33:34 字数 590 浏览 2 评论 0原文

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

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 显示为空。我错过了什么吗?

谢谢。

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 appears empty, although I checked that the request came in and languages were fetched. Am I missing something?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神也荒唐 2025-01-11 21:33:34

fetch 调用是异步的:

获取 collection.fetch([选项])

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

结果是,在 languages.fetch() 调用从服务器返回任何内容之前,您的 console.log languages.models 就被调用了。

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

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

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

initialize 中调用 languages.fetch 并将 @render 绑定到集合的 reset 事件会更有意义;然后当集合准备好时,您可以将内容放在页面上。

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

The fetch call is asynchronous:

fetch collection.fetch([options])

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.

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

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文