Is there a way I can make it easier to call my alert in the typescript code below. Here’s where my alert is coded:
module Admin.Shared.Alerts {
export class alerts {
static mvcOnFailure(message: string) {
dialog.alertWin("Internal Application Error", message);
}
}
}
Here is a small snippet example of where I call it:
module Admin.MyAccount.Access {
export function ajaxDone(data: string, textStatus: string, jqXHR: JQueryXHR) {
Admin.Shared.Alerts.alerts.mvcOnFailure("x");
}
}
The problem is that there is such a long string to call the alert. Is there some way that I can do something like a “using” in C# where I don’t have to specify “Admin.Shared.Alerts” ?
I have two recommendations for you in this respect. The first is that you reconsider your module structure. As soon as you see namespaces such as “Common”, “Shared” or “Core” it tells you there is a problem, because these are not words that describe what the code inside is likely to do. Good names are “Messaging”, “Alerts”, “Payments” – you can tell straight away what kind of stuff you will find there.
So recommendation number 1 is to remove the “Shared” namespace.
If you are building a large TypeScript program, you may want to follow the module patterns that are commonly used in CommonJS application. You use simple module names but nest them in folders:
.\Admin\Alerts.ts
You would import the module using
And then use it in your code under the
alertsname.In a web app, you would use a module loader, such as requires.js to handle all the loading for you so it wouldn’t be much additional work, and you would tell the compiler to create the code for you using a flag…