简体   繁体   English

通过对象方法链处理ajax成功回调的干净方法是什么?

[英]What's a clean way to handle ajax success callbacks through a chain of object methods?

So, I'm trying to improve my javascript skills and get into using objects more (and correctly), so please bear with me, here. 因此,我正在尝试提高我的JavaScript技能,并更多(正确地)使用对象,所以请在这里忍受。

So, take this example: http://jsfiddle.net/rootyb/mhYbw/ 因此,举个例子: http : //jsfiddle.net/rootyb/mhYbw/

Here, I have a separate method for each of the following: 在这里,我为以下每个方法提供了一个单独的方法:

  • Loading the ajax data 加载ajax数据
  • Using the loaded ajax data 使用加载的ajax数据

Obviously, I have to wait until the load is completed before I use the data, so I'm accessing it as a callback. 显然,我必须等到加载完成后才能使用数据,因此我将其作为回调进行访问。

As I have it now, it works. 就像我现在拥有的那样,它有效。 I don't like adding the initData callback directly into the loadData method, though. 我不喜欢将initData回调直接添加到loadData方法中。 What if I want to load data and do something to it before I use it? 如果我想在使用数据之前加载数据并对数据做些什么怎么办? What if I have more methods to run when processing the data? 如果在处理数据时有更多方法可以运行怎么办? Chaining this way would get unreadable pretty quickly, IMO. IMO,以这种方式进行链接将很快变得难以理解。

What's a better, more modular way of doing this? 有什么更好,更模块化的方式来做到这一点?

I'd prefer something that doesn't rely on jQuery (if there even is a magical jQuery way), for the sake of learning. 为了学习,我宁愿不依赖jQuery的东西(甚至还有一种神奇的jQuery方式)。

(Also, I'm sure I'm doing some other things horribly in this example. Please feel free to point out other mistakes I'm making, too. I'm going through Douglas Crockford's Javascript - The Good Parts, and even for a rank amateur, it's made a lot of sense, but I still haven't wrapped my head around it all) (此外,在这个示例中,我敢肯定我在做其他可怕的事情。请随时指出我正在犯的其他错误。我正在阅读道格拉斯·克罗克福德的Javascript-The Good Parts,甚至一个高级业余爱好者,这很有道理,但我仍然没有把头全部包裹住)

Thanks! 谢谢!

I don't see a lot that should be different. 我看不出很多不同之处。 I made an updated version of the fiddle here . 我在这里制作了小提琴的更新版本。 A few points I have changed though: 不过,我改变了几点:

  • Use the var keyword for local variables eg, self . var关键字用于局部变量,例如self
  • Don't add a temporary state as an object's state eg, ajaxData , since you are likely to use it only once. 不要将临时状态添加为对象的状态,例如ajaxData ,因为您可能只使用一次。
  • Encapsulate as much as possible: Instead of calling loadData with the object ajaxURL , let the object decide from which URL it should load its data. 尽可能地进行封装:不要让对象使用ajaxURL调用loadData ,而ajaxURL让对象决定应从哪个URL加载其数据。

One last remark: Don't try to meet requirements you don't have yet, even if they might come up in the future (I'm referring to your "What if...?" questions). 最后一句话:不要试图满足您尚未达到的要求,即使将来可能会出现(我指的是您的“如果...?”问题)。 If you try, you will most likely find out that you either don't need that functionality, or the requirements are slightly different from what you expected them to be in the past. 如果您尝试一下,很可能会发现您不需要该功能,或者要求与过去的期望稍有不同。 If you have a new requirement, you can always refactor your model to meet them. 如果您有新要求,则始终可以重构模型来满足它们。 So, design for change, but not for potential change. 因此,为变化而设计,而不是为潜在的变化而设计。

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

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