检索异步数据时正确的 Backbone.js 收集事件

发布于 2024-12-14 20:33:53 字数 430 浏览 0 评论 0原文

我想在从远程服务器异步加载集合时呈现视图。我有以下集合类

class BusinessUnits extends Backbone.Collection
  model: BusinessUnit

  parse: (units) ->
    units

然后我的观点是这样做的:

  load: (businessUnits) =>
    @collection = businessUnits
    @collection.fetch()
    @render()

显然 render() 将在获取完成之前被调用。

是否有一个在获取集合时触发的backbone.js 事件,或者我最好触发自己的事件?

这似乎是一个非常常见的场景。人们如何处理这种情况?

I want to render a view when a collection has been loaded asynchronously from the remote server. I have the following collection class

class BusinessUnits extends Backbone.Collection
  model: BusinessUnit

  parse: (units) ->
    units

And then my view I was doing this:

  load: (businessUnits) =>
    @collection = businessUnits
    @collection.fetch()
    @render()

Obviously render() will be invoked before the fetch has been completed.

Is there a backbone.js event that is fired whenever the collection is fetched or would I be better firing my own?

This seems like a very common scenario. How do people handle this type of situation?

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

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

发布评论

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

评论(1

青衫负雪 2024-12-21 20:33:53

我认为 "reset" 事件 就是您正在寻找的。

“重置”(集合)— 当集合的全部内容被替换时。

这将在获取完成后触发。

load: (businessUnits) =>
    @collection = businessUnits
    @collection.bind 'reset', => @render()
    @collection.fetch()

I think the "reset" event is what you are looking for.

"reset" (collection) — when the collection's entire contents have been replaced.

This will be triggered after the fetch completes.

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