简体   繁体   English

单击集合标题后,Backbonejs呈现集合

[英]Backbonejs rendering collection upon click on collection title

I am still using backbonejs since find it extremely easy and fast to bootstrap new micro API based website. 我仍然使用lobbjs,因为发现引导基于微API的新网站非常容易和快速。

Question is about best practice when it comes to collection loading and rendering upon the collection group click. 问题是有关在单击收藏夹组进行收藏夹加载和呈现时的最佳实践。

Lets say i have static list of brands. 可以说我有静态的品牌列表。 On click on the brand I initiate fetching of the collection and upon fetch end successful I render the collection. 点击品牌后,我便开始收集该收藏,并在成功结束后进行该收藏。

The problem happens when the user click on the brands not having the previous click processed, so what happens is that there are 2 collections og both brands loaded and rendered. 当用户单击未处理前次点击的品牌时,就会发生问题,因此发生的事情是加载和呈现了两个集合的品牌。 What is the best way to deal with this in order to "cancel" or just ignore the first fetch and only render the last one that user clicked? 为了“取消”或仅忽略第一个提取并仅呈现用户单击的最后一个提取,最好的处理方法是什么?

I found the solution is just deactivate the brand list until the request is processed, but it annoying UX wise because user might want to click right away on another one not waiting for the results (if he miss-clicked for example) 我发现解决方案只是停用品牌列表,直到处理完请求为止,但这使UX烦人,因为用户可能想立即单击另一个不等待结果的按钮(例如,如果他单击了错误的话)

The underlying xhr object returned from Backbone.fetch has the ability to cancel the current request, xhr.abort() . Backbone.fetch返回的基础xhr对象具有取消当前请求xhr.abort()

When the user clicks and the collection fetches, save the return value. 当用户单击并获取集合时,保存返回值。

var xhr = myCollection.fetch()

The xhr has properties that tell you the current status of the request. xhr具有告诉您请求当前状态的属性。

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

If the user clicks again and causes the collection to fetch while another request is still processing, simply ask the xhr if it's in the middle of a request. 如果用户再次单击并导致在仍在处理另一个请求时提取集合,则只需询问xhr是否在请求中间。 If it is, abort, and continue with your current fetch, and reassign the xhr object. 如果是,请中止操作,然后继续当前的访xhr ,然后重新分配xhr对象。

Sample code: 样例代码:

function userClickedSomething(){
    if(xhr.readyState > 0 && xhr.readyState < 4)
        xhr.abort()
    xhr = myCollection.fetch()
}

How you store a reference to the xhr object is up to you. 如何存储对xhr对象的引用取决于您。

More details: Is it possible to stop Backbone "read" requests 更多详细信息: 是否可以停止骨干“读取”请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM