Do you use resource file for storing your static script?

Updated by Brady Stroud [SSW] 1 year ago. See history

123
<introEmbed body={<> Resource files provide a structured and centralized approach to storing and retrieving static scripts, eliminating the need for scattered code snippets and enhancing your development workflow. </>} />
StringBuilder sb = new StringBuilder();
sb.AppendLine(@"<script type=""text/javascript"">");
sb.AppendLine(@"function deleteOwnerRow(rowId)");
sb.AppendLine(@"{");
sb.AppendLine(string.Format(@"{0}.Delete({0}.GetRowFromClientId(rowId));", OwnersGrid.ClientID));
sb.AppendLine(@"}");
sb.AppendLine(@"</script>");

❌ Figure: Bad example - Hard to read, the string is surrounded by rubbish + inefficient because you have an object and 6 strings

string.Format(@"
<script type=""text/javascript"">
function deleteOwnerRow(rowId)
{
{0}.Delete({0}.GetRowFromClientId(rowId));
}
</script>
", OwnersGrid.ClientID);

✅ Figure: Good example Slightly easier to read ?but it is 1 code statement across 10 lines

string scriptTemplate = Resources.Scripts.DeleteJavascript;
string script = string.Format(scriptTemplate, OwnersGrid.ClientID);
<script type=""text/javascript"">
function deleteOwnerRow(rowId)
{
{0}.Delete({0}.GetRowFromClientId(rowId));
}
</script>

Figure: The code in the first box, the string in the resource file in the 2nd box. This is the easiest to read + you can localize it eg. If you need to localize an Alert in the javascript

Image

Figure: Add a recourse file into your project in VS2005

Image

Figure: Read value from the new added resource file

Acknowledgements

Adam Cogan
Ryan Tee
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.

We open source.Loving SSW Rules? Star us on GitHub. Star