Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and maintaining software responsibly. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A while ago I inherited the front end on a product at work and I've been building further atop a somewhat confusing architecture of javascript. I finally have the time available, and the will from senior management, to dedicate some time to making this a bit more readable and maintainable.

This is a combo of ASP.NET MVC razor pages with javascript based around jQuery, some Kendo UI stuff, and knockout.js on some of the more complex pages.

Right now, every page that uses javascript calls an init line at the bottom like so. In this example you can see that it's also passing some data from the Model that was passed to the page. On the knockout pages, the data being passed into the js is the entire Model.

This doesn't seem entirely horrible, but I'm not quite sure if this is the right way to go about initializing the js.

@section Init
{
    fc.initProfileLocations(@(((bool) TempData["PreventZip"]).ToString().ToLower()),@(((bool)TempData["duplicateAddress"]).ToString().ToLower()));
}

The js itself is pretty much all like the following.

(function (f, $) {
    var pl = f.profilelocations = fc.profilelocations || {};
    f.initProfileLocations = function (preventZip, duplicateAddress) {
    //initialize a bunch of stuff like jquery plugins, event handlers, etc.
    };

    pl.onLocationSelect = function () {
        //stuff
    }
}(window.fc = window.fc || {}, jQuery));

Which is a sort of weird module pattern. I now have ES2015 modules available to me through Webpack but I'm not sure of the best way to utilize them as I've never used modules like CommonJS or AMD before. Especially since some pages are very simple and just need some initialization.

share|improve this question
2  
Frankly, your description of the architecture doesn't seem all that bad to me. If you insist on overhauling it, I suggest you go with a modern, MVVM framework like Aurelia. – Robert Harvey 2 days ago
    
If you're confused by the architecture try addressing that first. Understand why this particular architecture was chosen, and how it differs from other common javascript architectures. Then you can make a rational choice about whether to abandon it and what to replace it by. – Joeri Sebrechts 2 days ago
    
I agree with @RobertHarvey. One of the other things I would do is make your variable names more descriptive (unless that was for obfuscation of business data). Especially within your closure, f will be easy to forget what it means. Also, that looks like a fine way to pass data into the initialization method with Razor. – krillgar 23 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.