backbone.js - 如何以及何时显示微调器
主干中是否有任何类型的挂钩,我可以轻松地说“每当任何集合正在获取数据时,显示旋转器,完成后隐藏它”?
我有一种感觉,它会比这更复杂,并且需要覆盖特定的函数。我什么时候应该展示微调器?在 fetch()
或 refresh()
或其他什么上?
Is there any sort of hooks in backbone where I can easily say "whenever any of the collections is fetching data, show the spinner, hide it when they're done"?
I have a feeling it will be more complicated than that and require overwriting specific functions. When should I show the spinner? On fetch()
or refresh()
or something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 jQuery ajaxStart 和 ajaxStop。当发出 ajax 请求时,它们将全局运行,因此 fetch 和 save 将导致它们运行。添加代码以在开始时显示微调器并在最后隐藏它。
You can use jQuery ajaxStart and ajaxStop. Those will globally run when an ajax request is made, so fetch and save will cause those to run. Add your code to show the spinner in the start and hide it in the end.
在 Backbone.js 1.0.0 中,您可以使用
request
和sync
事件 http: //backbonejs.org/#Events-catalog这在视图中。
这可以应用于每个集合或每个模型,这是旋转器的一些 CSS http://abandon.ie/notebook /simple-loading-spinner-for-backbonejs
in Backbone.js 1.0.0 you can use the
request
andsync
events http://backbonejs.org/#Events-catalogThis goes in the view.
This can be applied per collection or per model here's some CSS for the spinner http://abandon.ie/notebook/simple-loading-spinner-for-backbonejs
当
Collection::fetch()
启动时,Backbone 不会触发任何事件 (查看源代码),因此您必须重写fetch
方法。也许是这样的:这将覆盖
fetch
方法,以便在获取开始时为您提供一个事件。但是,这只会触发特定集合实例上的事件,因此如果您有一堆不同的集合,则必须在每个集合上侦听该事件。Backbone doesn't trigger any event when
Collection::fetch()
starts (see source code), so you will have to override thefetch
method. Maybe something like this:This will override the
fetch
method to give you an event when the fetch starts. However, this only triggers the event on the specific collection instance so if you have a bunch of different collections you'll have to listen for that event on each collection.我在不覆盖主干的情况下完成此操作的方法是:
在视图中
另一条路线是在添加模型时删除加载类,通常您有:
在大多数情况下,这些几乎是相同的,因为加载器实际上是为了用户在显示内容之前删除它是有意义的。
The way i have done this without overriding backbone is:
In view
The other route would be to remove the loading class when the models are added, usually you have:
These would be almost identical in most cases, and as the loader is really for the users experience removing it just prior to displaying the content makes sense.
还有一点更新。自 2012 年 12 月 13 日起,Backbone.sync 中添加了一个
“request”
事件,每当开始向服务器发出请求时就会触发该事件。自 2012 年 1 月 30 日起,还添加了一个“sync”
事件,每当模型的状态与服务器成功同步(创建、保存、销毁)时就会触发该事件。因此,您不需要覆盖或扩展本机 Backbone 的方法。要侦听“开始/完成获取”事件,您可以将侦听器添加到模型/集合中,如下所示:
And a little update. Since Dec. 13, 2012 have been added a
"request"
event to Backbone.sync, which triggers whenever a request begins to be made to the server. As well since Jan. 30, 2012 have been added a"sync"
event, which triggers whenever a model's state has been successfully synced with the server (create, save, destroy).So, you don't need to override or extand the native Backbone's methodes. For listening 'start/finish fetching' event you can add listener to your model/collection like this for example:
您可以在任何模型上创建一个名为
sync
的方法,backbone.js 将调用该方法以进行同步。或者您可以简单地替换方法Backbone.sync。
这将允许您仅在源代码中的一处进行更改。You can create a method called
sync
on any of your models, and backbone.js will call that in order to sync. Or you can simply replace the methodBackbone.sync.
This will allow you to make the change in only one place in your source code.我在我的主干中使用了NProgress,它是功能最好的加载器/旋转器。
I have used NProgress in my backbone and it is the best functioning loader/spinner out there.
使用骨干同步方法,
它每次都会调用主干同步方法,不仅是获取、保存、更新和删除
/* 覆盖同步应用程序每个请求都会听到,除了直接 ajax */
Use Backbone sync method,
It will call every time backbone sync method, not only fetch, save, update and delete also
/* over riding of sync application every request come hear except direct ajax */