在 Backbone.js 集合上调用 fetch() 时会触发哪些事件? [英] What events are triggered when calling fetch() on a Backbone.js collection?

查看:24
本文介绍了在 Backbone.js 集合上调用 fetch() 时会触发哪些事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的backbone.js 应用程序中,有一个Trips 集合,其中包含Trip 模型,它与LocalStorage 一起使用.我可以调用 Trips.create(form_attributes) 来创建并保存到 Todos 商店 的行程.

In my backbone.js app, there is a Trips collection that holds Trip models, which is working with LocalStorage. I am able to call Trips.create(form_attributes) to create and save a trip to the Todos store.

当我第一次加载我的应用程序时,我调用 Trips.fetch({ success: trips_fetch_success }),并且 trips_fetch_success 收到一个显示 Trip 模型的响应 Trips 集合 保存.

When I first load my app, I call Trips.fetch({ success: trips_fetch_success }), and trips_fetch_success receives a response that shows the Trip models that the Trips collection holds.

我尝试将 refreshchange 事件绑定到 Trips 集合,但这些事件没有被捕获,这让我相信我对 Trips.fetch 触发哪些事件有错误的想法.

I have tried to bind refresh and change events to the Trips collection, but these events are not being caught, making me believe I have the wrong idea about which events Trips.fetch triggers.

我的问题:Trips.fetch 应该触发哪些事件?事件是在集合上触发还是在每个单独的 Trip 模型 上触发?

My question: which events should Trips.fetch trigger? And are the events triggered on the collection or on each of the individual Trip models?

推荐答案

Collection.fetch() 将在成功时调用 reset ,这反过来会触发 'reset' 事件.集合重置事件的任何订阅者都应该收到该事件.

Collection.fetch() will call reset on success, which in turn will trigger a 'reset' event. Any subscribers to the collections reset event should receive the event.

这里的关键是成功".我遇到了这个问题,只是发现主干正在默默地吞下我的错误消息.传入至少记录到 console.log() 的错误处理程序,然后查看发生了什么:

The key here is "on success." I had this problem, only to discover that backbone was silently swallowing my errors messages. Pass in an error handler that, at least, logs to console.log(), and see what's happening:

trips.fetch({error: function() { console.log(arguments); }});

(注意:旧版本的backbone.js 会触发刷新"而不是重置")

(Note: Old versions of backbone.js will trigger "refresh" instead of "reset")

这篇关于在 Backbone.js 集合上调用 fetch() 时会触发哪些事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆