简体   繁体   English

嵌套方法,最佳做法

[英]Nested methods, best practice

I'm currently fiddling around with jQuery's ajax method to get some data from a JSON feed and animating some bars according to the data. 我目前正在摆弄jQuery的ajax方法来从JSON提要中获取一些数据,并根据该数据设置一些动画。

As I'm hacking away, I don't really know if my approach is right. 当我要搬走时,我真的不知道我的方法是否正确。

$.ajax({

    url: "/echo/json/",
    type: "POST",
    data: data,
    success: function(data){


    $("#value1-bar .usage-bar-fill").animate({
        width: data.Value1
        }, 2500, function() {
        // Animation complete.
    });

    $("#value2-bar .usage-bar-fill").animate({
        width: data.Value2
        }, 2500, function() {
        // Animation complete.
    });

    },
    dataType: "json"
});

Is it OK to animate the divs from inside the ajax method, or is there a more correct way to do this - formatting wise, memory wise etc.? 是否可以从ajax方法内部对div进行动画处理,还是可以使用更正确的方法-格式化,内存格式化等?

Is it better to some how return some variables from the ajax method and executing the animation method from outside the ajax method? 更好地说明如何从ajax方法返回一些变量并从ajax方法之外执行动画方法吗?

Check out jsfiddle example here: http://jsfiddle.net/timkl/KPvCj/ 在此处查看jsfiddle示例: http//jsfiddle.net/timkl/KPvCj/

I know this is a pretty broad question, but being a graphic designer come self-taught web designer - I never really know if what I'm doing is good practice. 我知道这是一个相当广泛的问题,但是作为一名平面设计师来做自学成才的Web设计师-我从不真正知道我在做什么是否是一种好习惯。

I like this question, and I'd love to see some more detailed feedback, but I'd say it looks good. 我喜欢这个问题,也希望看到一些更详细的反馈,但我认为它看起来不错。 You're already calling an embedded success function within the ajax method, so worrying about memory / performance of further embedded functions seems almost negligible... 您已经在ajax方法中调用了嵌入式成功函数,因此担心内存/其他嵌入式函数的性能似乎可以忽略不计...

If you're going to want to animate your usage-bar-fill s some more with the same / similar animation you used within your success function, then I might create separate functions and call those instead...but I'm sure you knew that already ;) 如果您想usage-bar-fill您在成功函数中使用的相同/相似动画为您的“ usage-bar-fill更多的动画,那么我可以创建单独的函数并调用它们……但是我确定您会已经知道了;)

Nice question! 好问题!

This is nearly a question of taste, since this seems to be a very small application. 这几乎是一个品味问题,因为这似乎是一个很小的应用程序。 When your applications grow, overall design and principles becomes a more crucial part of the process. 当您的应用程序增长时,总体设计和原则将成为过程中更关键的部分。

But that said, I don't like this approach. 但这就是说,我不喜欢这种方法。 It is very prone to produce code that repeats itself unnecessarily - unless these are strictly one-off situations that are very specific to this use case, but most of the time you can generalize to enable code reuse. 产生不必要的重复代码非常容易-除非这些情况是严格针对此用例的一次性情况,但是在大多数情况下,您可以概括化以实现代码重用。

Personally, I would declare the callback functions elsewhere, probably on an object which already knows what it should be working with (ie your selector) and knows what to do with it. 就我个人而言,我将在其他地方声明回调函数,可能在一个已经知道应该使用的对象(即您的选择器)并且知道如何使用它的对象上。 This way, the code almost structures itself, keeping only the very locally relevant bits where they are needed. 这样,代码几乎可以自行构造,只在需要它们的地方保留非常相关的位。

In this specific case, it would be something along the lines of a graph object, which when set a value updates its own display/view appropriately. 在这种特定情况下,它可能是图形对象的东西,当设置一个值时,它将适当地更新其自己的显示/视图。

Hopefully my ramblings make sense. 希望我的闲话有道理。 I might be a tad too tired to give out opinions and expert advice, so I'll just stop here. 我可能有点累,无法给出意见和专家建议,所以我就在这里停止。

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

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