Simple AJAX view loading approach

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I'm writing my own MVC framework for learning purposes. Usually in my projects I like to use AJAX to send requests and retrive data. Now my doubt is if this can be a best approach to make a one page dashboard.



I've created an .htaccess who will display only folders that have an index file inside, the dashboard will be loaded only after user login, the login form is on the index file who is inside a folder named user. On the root of the project there is another index file who will be publicy available.



Assuming I've this code in the user dashboard file:



HTML



<div class="container-fluid" id="app">

<div class="row">

<div class="col-sm-12 col-lg-4">

<a href="#" id="open-settings">
<div class="card">
<div class="card-body">
<div class="card-img-top text-center">
<i class="fas fa-cog fa-3x"></i>
</div>
<h5 class="card-title text-uppercase text-center">settings</h5>
</div>
</div>
</a>
</div>

<div class="col-sm-12 col-lg-4">
<a href="#">
<div class="card">
<div class="card-body">
<div class="card-img-top text-center">
<i class="fas fa-database fa-3x"></i>
</div>
<h5 class="card-title text-uppercase text-center">show database entry</h5>
</div>
</div>
</a>
</div>

<div class="col-sm-12 col-lg-4">
<a href="#">
<div class="card">
<div class="card-body">
<div class="card-img-top text-center">
<i class="fas fa-plus fa-3x"></i>
</div>
<h5 class="card-title text-uppercase text-center">new insert</h5>
</div>
</div>
</a>
</div>
</div>
<div class="row" id="display-view"></div>
</div> <!-- container end -->


What is the best approach to load the requested view for the user selected action?
I want to avoid duplication of the jquery code who now is something like this



$('#open-settings').on('click',function(e){
e.preventDefault();
preloader.modal('show');
$.ajax(
type: 'GET',
url: 'view/settings.php',
cache: false,
beforeSend: function()
preloader.modal('show');
,
success: function(response)
$('#display-view').html(response)

);






share|improve this question



























    up vote
    0
    down vote

    favorite












    I'm writing my own MVC framework for learning purposes. Usually in my projects I like to use AJAX to send requests and retrive data. Now my doubt is if this can be a best approach to make a one page dashboard.



    I've created an .htaccess who will display only folders that have an index file inside, the dashboard will be loaded only after user login, the login form is on the index file who is inside a folder named user. On the root of the project there is another index file who will be publicy available.



    Assuming I've this code in the user dashboard file:



    HTML



    <div class="container-fluid" id="app">

    <div class="row">

    <div class="col-sm-12 col-lg-4">

    <a href="#" id="open-settings">
    <div class="card">
    <div class="card-body">
    <div class="card-img-top text-center">
    <i class="fas fa-cog fa-3x"></i>
    </div>
    <h5 class="card-title text-uppercase text-center">settings</h5>
    </div>
    </div>
    </a>
    </div>

    <div class="col-sm-12 col-lg-4">
    <a href="#">
    <div class="card">
    <div class="card-body">
    <div class="card-img-top text-center">
    <i class="fas fa-database fa-3x"></i>
    </div>
    <h5 class="card-title text-uppercase text-center">show database entry</h5>
    </div>
    </div>
    </a>
    </div>

    <div class="col-sm-12 col-lg-4">
    <a href="#">
    <div class="card">
    <div class="card-body">
    <div class="card-img-top text-center">
    <i class="fas fa-plus fa-3x"></i>
    </div>
    <h5 class="card-title text-uppercase text-center">new insert</h5>
    </div>
    </div>
    </a>
    </div>
    </div>
    <div class="row" id="display-view"></div>
    </div> <!-- container end -->


    What is the best approach to load the requested view for the user selected action?
    I want to avoid duplication of the jquery code who now is something like this



    $('#open-settings').on('click',function(e){
    e.preventDefault();
    preloader.modal('show');
    $.ajax(
    type: 'GET',
    url: 'view/settings.php',
    cache: false,
    beforeSend: function()
    preloader.modal('show');
    ,
    success: function(response)
    $('#display-view').html(response)

    );






    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm writing my own MVC framework for learning purposes. Usually in my projects I like to use AJAX to send requests and retrive data. Now my doubt is if this can be a best approach to make a one page dashboard.



      I've created an .htaccess who will display only folders that have an index file inside, the dashboard will be loaded only after user login, the login form is on the index file who is inside a folder named user. On the root of the project there is another index file who will be publicy available.



      Assuming I've this code in the user dashboard file:



      HTML



      <div class="container-fluid" id="app">

      <div class="row">

      <div class="col-sm-12 col-lg-4">

      <a href="#" id="open-settings">
      <div class="card">
      <div class="card-body">
      <div class="card-img-top text-center">
      <i class="fas fa-cog fa-3x"></i>
      </div>
      <h5 class="card-title text-uppercase text-center">settings</h5>
      </div>
      </div>
      </a>
      </div>

      <div class="col-sm-12 col-lg-4">
      <a href="#">
      <div class="card">
      <div class="card-body">
      <div class="card-img-top text-center">
      <i class="fas fa-database fa-3x"></i>
      </div>
      <h5 class="card-title text-uppercase text-center">show database entry</h5>
      </div>
      </div>
      </a>
      </div>

      <div class="col-sm-12 col-lg-4">
      <a href="#">
      <div class="card">
      <div class="card-body">
      <div class="card-img-top text-center">
      <i class="fas fa-plus fa-3x"></i>
      </div>
      <h5 class="card-title text-uppercase text-center">new insert</h5>
      </div>
      </div>
      </a>
      </div>
      </div>
      <div class="row" id="display-view"></div>
      </div> <!-- container end -->


      What is the best approach to load the requested view for the user selected action?
      I want to avoid duplication of the jquery code who now is something like this



      $('#open-settings').on('click',function(e){
      e.preventDefault();
      preloader.modal('show');
      $.ajax(
      type: 'GET',
      url: 'view/settings.php',
      cache: false,
      beforeSend: function()
      preloader.modal('show');
      ,
      success: function(response)
      $('#display-view').html(response)

      );






      share|improve this question













      I'm writing my own MVC framework for learning purposes. Usually in my projects I like to use AJAX to send requests and retrive data. Now my doubt is if this can be a best approach to make a one page dashboard.



      I've created an .htaccess who will display only folders that have an index file inside, the dashboard will be loaded only after user login, the login form is on the index file who is inside a folder named user. On the root of the project there is another index file who will be publicy available.



      Assuming I've this code in the user dashboard file:



      HTML



      <div class="container-fluid" id="app">

      <div class="row">

      <div class="col-sm-12 col-lg-4">

      <a href="#" id="open-settings">
      <div class="card">
      <div class="card-body">
      <div class="card-img-top text-center">
      <i class="fas fa-cog fa-3x"></i>
      </div>
      <h5 class="card-title text-uppercase text-center">settings</h5>
      </div>
      </div>
      </a>
      </div>

      <div class="col-sm-12 col-lg-4">
      <a href="#">
      <div class="card">
      <div class="card-body">
      <div class="card-img-top text-center">
      <i class="fas fa-database fa-3x"></i>
      </div>
      <h5 class="card-title text-uppercase text-center">show database entry</h5>
      </div>
      </div>
      </a>
      </div>

      <div class="col-sm-12 col-lg-4">
      <a href="#">
      <div class="card">
      <div class="card-body">
      <div class="card-img-top text-center">
      <i class="fas fa-plus fa-3x"></i>
      </div>
      <h5 class="card-title text-uppercase text-center">new insert</h5>
      </div>
      </div>
      </a>
      </div>
      </div>
      <div class="row" id="display-view"></div>
      </div> <!-- container end -->


      What is the best approach to load the requested view for the user selected action?
      I want to avoid duplication of the jquery code who now is something like this



      $('#open-settings').on('click',function(e){
      e.preventDefault();
      preloader.modal('show');
      $.ajax(
      type: 'GET',
      url: 'view/settings.php',
      cache: false,
      beforeSend: function()
      preloader.modal('show');
      ,
      success: function(response)
      $('#display-view').html(response)

      );








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 28 at 14:58









      Your Common Sense

      2,405523




      2,405523









      asked Jun 28 at 12:10









      user9741470

      1009




      1009




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Without really knowing a great deal about how your code is structured and basing it purely on your ajax function you could so somethhing like this;



          function loadView(url, beforeSend, callback) 
          $.ajax(
          type: 'GET',
          url: url,
          cache: false,
          beforeSend: function()
          beforeSend();
          ,
          success: function(response)
          callback(response);

          );



          You could then use it like this



          var showPreLoaderModal = function() 
          preloader.modal('show');


          loadView("view/settings.php", showPreLoaderModal, function(response)
          $('#display-view').html(response)
          );


          But this is dirty the modern approach would involve promises!






          share|improve this answer





















          • I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
            – user9741470
            Jun 28 at 15:50










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197427%2fsimple-ajax-view-loading-approach%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          Without really knowing a great deal about how your code is structured and basing it purely on your ajax function you could so somethhing like this;



          function loadView(url, beforeSend, callback) 
          $.ajax(
          type: 'GET',
          url: url,
          cache: false,
          beforeSend: function()
          beforeSend();
          ,
          success: function(response)
          callback(response);

          );



          You could then use it like this



          var showPreLoaderModal = function() 
          preloader.modal('show');


          loadView("view/settings.php", showPreLoaderModal, function(response)
          $('#display-view').html(response)
          );


          But this is dirty the modern approach would involve promises!






          share|improve this answer





















          • I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
            – user9741470
            Jun 28 at 15:50














          up vote
          1
          down vote













          Without really knowing a great deal about how your code is structured and basing it purely on your ajax function you could so somethhing like this;



          function loadView(url, beforeSend, callback) 
          $.ajax(
          type: 'GET',
          url: url,
          cache: false,
          beforeSend: function()
          beforeSend();
          ,
          success: function(response)
          callback(response);

          );



          You could then use it like this



          var showPreLoaderModal = function() 
          preloader.modal('show');


          loadView("view/settings.php", showPreLoaderModal, function(response)
          $('#display-view').html(response)
          );


          But this is dirty the modern approach would involve promises!






          share|improve this answer





















          • I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
            – user9741470
            Jun 28 at 15:50












          up vote
          1
          down vote










          up vote
          1
          down vote









          Without really knowing a great deal about how your code is structured and basing it purely on your ajax function you could so somethhing like this;



          function loadView(url, beforeSend, callback) 
          $.ajax(
          type: 'GET',
          url: url,
          cache: false,
          beforeSend: function()
          beforeSend();
          ,
          success: function(response)
          callback(response);

          );



          You could then use it like this



          var showPreLoaderModal = function() 
          preloader.modal('show');


          loadView("view/settings.php", showPreLoaderModal, function(response)
          $('#display-view').html(response)
          );


          But this is dirty the modern approach would involve promises!






          share|improve this answer













          Without really knowing a great deal about how your code is structured and basing it purely on your ajax function you could so somethhing like this;



          function loadView(url, beforeSend, callback) 
          $.ajax(
          type: 'GET',
          url: url,
          cache: false,
          beforeSend: function()
          beforeSend();
          ,
          success: function(response)
          callback(response);

          );



          You could then use it like this



          var showPreLoaderModal = function() 
          preloader.modal('show');


          loadView("view/settings.php", showPreLoaderModal, function(response)
          $('#display-view').html(response)
          );


          But this is dirty the modern approach would involve promises!







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 28 at 14:54









          Dan

          373211




          373211











          • I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
            – user9741470
            Jun 28 at 15:50
















          • I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
            – user9741470
            Jun 28 at 15:50















          I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
          – user9741470
          Jun 28 at 15:50




          I would to use a PHP approach but I don't have idea on how to start to make a class that render the needed views.
          – user9741470
          Jun 28 at 15:50












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197427%2fsimple-ajax-view-loading-approach%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods