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.
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.?
Is it better to some how return some variables from the ajax method and executing the animation method from outside the ajax method?
Check out jsfiddle example here: 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.
no, there's nothing wrong with that at all, and in js, it's usually a good thing. the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling.
The max nesting level is 32 Nesting Stored Procedures.
What is a nested method. If a method is defined inside another method, the inner method is said to be nested inside the outer method or it is called Nested method. All languages do not support nesting a method inside another method but python allows you to do so.
Nested (or inner, nested) functions are functions that we define inside other functions to directly access the variables and names defined in the enclosing function. Nested functions have many uses, primarily for creating closures and decorators.
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...
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 ;)
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 (i.e. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With