Javascript Implementation Patterns for Server-side MVC Websites

I’m looking for information on common patterns for initializing and executing Javascript page by page in a “traditional” server-side MVC website architecture.

A few months ago, my development team began, but abandoned, a major re-architecture of our company’s primary web app, including a full front-end redesign. In the process, there was some debate about the architecture of the Javascript in the current version of the site, and whether it fit into a clear, modern design pattern.

Now I’ve returned to the process of overhauling the front end of this and several other MVC websites (Ruby on Rails and MVC.net) to implement a responsive framework (Bootstrap), and in the process will again need to review then revamp and update a lot of Javascript.

These web applications are NOT single-page Javascript applications (in fact, we are ripping out a lot of Ajax) or designed to require a Javascript MVC pattern; these apps are basically brochure, catalog and administrative sites that follow a server-side MVC pattern. The vast majority of the Javascript required is behavioral, pre-built plugins (JQuery and Bootstrap, et al) that execute on specific DOM nodes.

I’m going to give a very brief (as brief as I can be) run-down of the current architecture only in order to illustrate the scope and type of paradigm I’m talking about. Hopefully, it will help you understand the nature of the patterns I’m looking for, but I’m not looking for commentary on the specifics of this code.

What I’ve done in the past is relatively straight-forward and easy to maintain, but, as mentioned above, some of the other developers don’t like the current architecture.

Currently, on document ready, I execute whatever global Javascript needs to occur on every page, and then call a page-specific init function to initialize node-specific functionality, retrieving the init method from a JS object.

On each page load, something like this will happen:

$(document).ready(function(){
  $('header').menuAction();
  App.pages.executePage('home','show'); //dynamic from framework request object
});

And the main App javascript is like

App = {
  usefulGlobalVar: 0,
  pages:
  {
    executePage: function(action, controller)
    { 
       // if exists, App.pages[action][controller].init()
    },
    home:
    {
      show:
      {
         init: function()
         {
            $('#tabs').tabs(); //et. al
         },
         normalizeName: function()
         {
            // dom-specific utility function that
            // doesn't require a full-blown component/class/module
         }
      },
      edit:
          ...
    },
    user_profile:
      ...
  }
}

Any common features and functionality requiring modularization or compotentizing is done as needed with prototyping. For common implementation of plugins, I often extend JQuery, so I can easily initialize a plugin with the same options throughout the site.

For example,

$('[data-tabs]').myTabs()

with this code in a utility javascript file:

 (function($) {
      $.fn.myTabs = function() {
        this.tabs( {
          //...common options
        });
      };
    })

Pointers to articles, books or other discussions would be most welcome. Again, I am looking for a site-wide implementation pattern, NOT a JS MVC framework or general how-tos on creating JS classes or components.

Thanks for your help!

1

Well, it’s really hard to suggest a pattern without knowing your exact Requirements. I’ve personally used Revealing Module Pattern in couple of applications — for some big international organizations, and the development team is quite happy with it. But as I said before, it really depends to your requirements.

Generally I can recommend you to take a look at Addy Osmani’s JavaScript book to somehow get more familiar with what options you have.

To me both Module and Revealing Module patterns sounds like a good fit for an MVC backend.

Here is kinda similar to what I usually use:

/* main.js */
( function ( window ) {
    window.App = {
        ...
    }
})( window );

/* moduleA.js */
( function ( App ) {
    App.functionA = {
        ...
    }
})( App );

/* moduleB.js */
( function ( App ) {
    App.functionB = {
        ...
    }
})( App );

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Javascript Implementation Patterns for Server-side MVC Websites

I’m looking for information on common patterns for initializing and executing Javascript page by page in a “traditional” server-side MVC website architecture.

A few months ago, my development team began, but abandoned, a major re-architecture of our company’s primary web app, including a full front-end redesign. In the process, there was some debate about the architecture of the Javascript in the current version of the site, and whether it fit into a clear, modern design pattern.

Now I’ve returned to the process of overhauling the front end of this and several other MVC websites (Ruby on Rails and MVC.net) to implement a responsive framework (Bootstrap), and in the process will again need to review then revamp and update a lot of Javascript.

These web applications are NOT single-page Javascript applications (in fact, we are ripping out a lot of Ajax) or designed to require a Javascript MVC pattern; these apps are basically brochure, catalog and administrative sites that follow a server-side MVC pattern. The vast majority of the Javascript required is behavioral, pre-built plugins (JQuery and Bootstrap, et al) that execute on specific DOM nodes.

I’m going to give a very brief (as brief as I can be) run-down of the current architecture only in order to illustrate the scope and type of paradigm I’m talking about. Hopefully, it will help you understand the nature of the patterns I’m looking for, but I’m not looking for commentary on the specifics of this code.

What I’ve done in the past is relatively straight-forward and easy to maintain, but, as mentioned above, some of the other developers don’t like the current architecture.

Currently, on document ready, I execute whatever global Javascript needs to occur on every page, and then call a page-specific init function to initialize node-specific functionality, retrieving the init method from a JS object.

On each page load, something like this will happen:

$(document).ready(function(){
  $('header').menuAction();
  App.pages.executePage('home','show'); //dynamic from framework request object
});

And the main App javascript is like

App = {
  usefulGlobalVar: 0,
  pages:
  {
    executePage: function(action, controller)
    { 
       // if exists, App.pages[action][controller].init()
    },
    home:
    {
      show:
      {
         init: function()
         {
            $('#tabs').tabs(); //et. al
         },
         normalizeName: function()
         {
            // dom-specific utility function that
            // doesn't require a full-blown component/class/module
         }
      },
      edit:
          ...
    },
    user_profile:
      ...
  }
}

Any common features and functionality requiring modularization or compotentizing is done as needed with prototyping. For common implementation of plugins, I often extend JQuery, so I can easily initialize a plugin with the same options throughout the site.

For example,

$('[data-tabs]').myTabs()

with this code in a utility javascript file:

 (function($) {
      $.fn.myTabs = function() {
        this.tabs( {
          //...common options
        });
      };
    })

Pointers to articles, books or other discussions would be most welcome. Again, I am looking for a site-wide implementation pattern, NOT a JS MVC framework or general how-tos on creating JS classes or components.

Thanks for your help!

1

Well, it’s really hard to suggest a pattern without knowing your exact Requirements. I’ve personally used Revealing Module Pattern in couple of applications — for some big international organizations, and the development team is quite happy with it. But as I said before, it really depends to your requirements.

Generally I can recommend you to take a look at Addy Osmani’s JavaScript book to somehow get more familiar with what options you have.

To me both Module and Revealing Module patterns sounds like a good fit for an MVC backend.

Here is kinda similar to what I usually use:

/* main.js */
( function ( window ) {
    window.App = {
        ...
    }
})( window );

/* moduleA.js */
( function ( App ) {
    App.functionA = {
        ...
    }
})( App );

/* moduleB.js */
( function ( App ) {
    App.functionB = {
        ...
    }
})( App );

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật