I'm using twitter mobile and I have a login-dialog. login-Dialog is a modal dialog. To call it the user calls loginDialog.show();
I'm not all that comfortable with the coding style I've adopted (i'm very much self schooled in JavaScript) and I'd be interested to hear peoples opinions on the code structure and process handling. For example I always seem to use expressive functions instead of function declarations. (some of the nitty gritty is ommitted for focus).
var loginDialog = (function($)
{
/**
* Essentially the init function
*/
var show = function()
{
// This actually shows the dialog
$("#login-dialog").modal({backdrop: "static", keyboard: false});
applyBindings();
}
var hide = function()
{
$("#login-dialog").modal("hide");
}
var applyBindings = function()
{
$("#login-dialog .btn-primary").click(onLoginClick);
};
var onLoginClick = function()
{
// validate form, attempt login...
// logginIn is a jQuery Deferred.
var loggingIn = myApi.login(username, password);
loggingIn.done( handleResponse );
loggingIn.fail( handleError );
};
var validate = function(username, password)
{
// simple form validation
};
var handleResponse = function(json)
{
// check json psuedo code
if (json != "Ok")
{
handleError();
}
else
{
handleSuccess();
}
};
var handleError = function(msg)
{
msg = msg || "There was a problem communicating with the server";
var tmpl = _.template($("#alert-tmpl").html(), { head: "Error", body: msg});
$("#login-dialog form").append(tmpl);
$("#login-dialog .btn-primary").removeAttr("disabled");
};
var handleSuccess = function()
{
hide();
};
return {
show: show,
hide: hide
}
})(jQuery);
returnand the following brace - if you did, the function would always returnundefined. You probably know this, which is why you wrote it. Trouble is that the style then becomes inconsistent. But it's your call; just know that style can cause bugs in JS. Speaking of consistency, you might want to add some semis after the function declarations. There's one afterapplyBindingsbut it's random. \$\endgroup\$