Search code examples
javascriptangularjsajaxsmartadmin

SmartAdmin Template Pagefunction runs twice Bug


I am developing web application in smartAdmin Template, which is fully ajax based template you can check demo here. i am facing some difficulties in this template. when i write one javascript function on some page it works on all pages.

for example

$(document).on('click', '#elementA', function(){
 alert('Hello World');
});

works on other pages's element which also have same id, it is difficult to give different ids to all element as it is very large project and i am working on it since 6 months, so i thought about it and find out solution to give unique id to each pages and write script like this.

$(document).on('click', '#pageA #elementA', function(){
   alert('Hello World');
});

i thought i solved the issue but, isn't function stopped working on other page's element. but when i visit #PageA 2nd time the function runs twice. actually template stores all the user defined function in local memory storage (i think, i am not sure about this) and keeps storing, until we do not refresh whole template.


Solution

  • ok, after long R&D i solved this my self.. i used loadscript() function to prevent loading scripts twice unnecessarily..

    i wrote all the script into one file (now i will have two view pages for one page.)

    earlier it was like.. A.php -> JScript + PHP & HTML now it is like A.php -> PHP & HTML, script/A.php -> OnlyJS

    as i am using codeginiter framework, and dont want others too see js by accessing it through url, i used this process.

    code on my view file

    loadScript("<?php echo site_url('processor/load_script/path_to_folder/script/add'); ?>");
    

    function on Processor controller

    public function load_script($path)
    {
        $last_segment = count($this->uri->segment_array());
        $path = '';
        for($i=3;$i<=$last_segment;$i++)
        {
            $path .= '/'.$this->uri->segment($i);
        }
        $this->load->view('core/ajax'.$path);
    }