Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a Javascript class with dynamic functions
    primarykey
    data
    text
    <p>Let's start by saying that my code works perfectly fine, there is no problem with it. I just want to wrap it all up with a nice class for dynamic future use, and I wonder how to do that in Javascript in the most correct way.</p> <p>load_server is a function that puts an Ajax request. pagination() / itemlinks() are functions that go through the retrieved data and marks links for future Ajax use. I currently write everything in functions and just dump it in the code, but I would like to create something like:</p> <pre><code>function ajaxRequest(type, link, container) { this.after_success = null; this.check_pagination = true; if(typeof(type)) == ‘undefined’) this.type=''; if(typeof(link)) == ‘undefined’) this.link=''; if(typeof(container)) == ‘undefined’) this.container=''; this.pagination=function() { //static function.. }; this.load_server=function () { //function logic here.. }; while(mainReq.after_success) { func(); } } var mainReq = new ajaxRequest{'link'}; mainReq.after_success = { itemlinks = function() { }, morefunc = function() { } }; mainReq.submit(); </code></pre> <p>I currently use the following jQuery code:</p> <pre><code>load_server = function (type, link, container) { $(container).html("&lt;div class='mask-loading'&gt;Loading ...&lt;/div&gt;"); $(container).load(getUrl, function(responseText, textStatus, XMLHttpRequest) { //Callback function. if(textStatus=='success') { pagination_render(); itemlinks(); } else { $(container).html("We are sorry, but there was an error with your request.&lt;br /&gt;Please try again."); } }); } var pagination_render = function() { var pagination = $('.pagination a'); pagination.each(function() { $(this).click(function(event) { console.log(this.href); load_server('page', this.href, '#tab1'); return false; }); }); }; pagination_render(); //init any pagination files </code></pre> <p>The other functions are the same, so no need to splash them around..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload