Well, I suppose one could call "API.roundCorners" function (and do
feature test) at the time of creating element (var myElt = /* ... */
<- here, I suppose), so that "API.cloneElement" would return element
that already has rounded element (or not, if feature is not
available).
This approach seems better to me, since there is separation of concern
(Ajax - styling).
Any thoughts about this?
Now I see I've made a mistake (and was unclear about what I was
thinking). It's not "has rounded element" but "has rounded corners".
Sorry about that, I haven't drank my first coffee at the time I was
writing post, so my brain wasn't functional.
If this:
"Of course it's easy enough to remove the
initial API.roundCorners check and add it around the one line where
it's needed"
does mean adding test like this (as I understood):
var myForm = /* ... */,
myElt = /* ... */,
myContainer = /* ... */;
if (API.attachEvent && API.validateForm && API.xhr &&
API.cloneElt && API.appendElt) {
API.attachEvent(myForm, "submit", function(evt) {
if (API.validateForm(myForm) {
API.xhr({
method: "POST",
url: myForm.action,
success: function(data) {
var newElt = API.cloneElement(myElt);
// update newElt using data;
API.appendElt(myContainer, newElt);
if (API.roundCorners) API.roundCorders(newElt);
},
error: function(msg) {/* ... */}
})
}
});
}
Maybe, something like this would be little better:
var myForm = /* ... */,
myContainer = /* ... */;
var myElt = /*...*/;
if (API.roundCorners) {
API.roundCorners(myElt);
}
if (API.attachEvent &&API.validateForm && API.xhr &&
API.cloneElt && API.appendElt) {
API.attachEvent(myForm, "submit", function(evt) {
if (API.validateForm(myForm) {
API.xhr({
method: "POST",
url: myForm.action,
success: function(data) {
var newElt = API.cloneElement(myElt);
// update newElt using data;
API.appendElt(myContainer, newElt);
},
error: function(msg) {/* ... */}
})
}
});
}
This way Ajax functionality isn't "polluted" with styling
functionality.
***
This isn't so much important, but me personally would make some kind
of separation between "this" and "that" functionality.
I'm no JS expert, so I don't know is this good approach (in some other
languages it is). Any thoughts, this time?