lundi 31 août 2015

Is this scope and javascript oop approach is correct?

I am developing mvc project and I am writing jQuery functions very much per page and it's getting complex while function counts incresing. Finally page header looks very dirty.

I decided using java-script's oop, I wrote this code:

var thisPage = function () {
        var page = this;
        page.showLoader = function () { //show loader codes };
        page.hideLoader = function () { //hide loader codes };
        page.database = function () { };
        page.database.getSomethingFromDb = function (campaignId) {
            //Get some data from db
        };
        page.database.getSomethingFromDb2 = function (campaignID) {
             //Get some data from db
        };
        page.database.getSomethingFromDb3 = function (campaignID) {
             //Get some data from db
        };
        page.multiselects = function () 
        { 
          this.multiselects.campaigns(); 
          this.multiselects.teams(); 
        };
        page.multiselects.campaigns = function () {
            //Kampanya listesi
            $("#campaigns-list").on("change", function () {
                page.database.getTeamsByCampaignId(selectedCampaignId);
                page.database.getProductsByCampaignId(selectedCampaignId);
            });
        };
        page.multiselects.teams = function () {
            $("#team-list").on("change", function () {
                page.database.getUsersByTeamId(selectedTeamId);
            });

        };
        ///... some more functions which seperated by subclass in thisPage class
    };
    $(document).ready(function () {
      //Get new instance from thisPage class
        var page = new thisPage();

        //Bind multiselects
        page.multiselects();

        //Maybe I could call some another thisPage class's functions...

    });

I thought this approach keep my code readable and editable.

What do you guys think on this kind of approach ?

Should I keep it ?

Aucun commentaire:

Enregistrer un commentaire