lonnieezell / Bonfire

Jumpstart your CodeIgniter web applications with a modular, HMVC-ready, backend.
http://cibonfire.com
1.39k stars 525 forks source link

How to use different javascript for different views of the same module? #1263

Open moxuanyuan opened 7 years ago

moxuanyuan commented 7 years ago

Can the bonfire core to improve this issue ?

I has spent too much time to solve this issue until I found the following article

http://speakingofcomputers.blogspot.com/2012/05/bonfire-adding-javascript-and-css-to.html

You can include the javascript file in a module view by using the Assets library in bonfire (codeigniter). Suppose you want to include a javascript file called ‘custom_edit_account.js’ inside the ‘account’ module. So follow these steps: 1) Create a folder called ‘assets’ in account module. So the path of assets folder is /bonfire/modules/account/assets. 2) Inside the assets folder created in step 1,create a ‘js’ folder so that the path of the js folder is /bonfire/modules/account/assets/js 3) Now place the custom_edit_user.js inside the ‘js’ folder. So the path is /bonfire/modules/account/assets/js/ customedit account.js 4) Now inside the controllers action whose view you want to include the js file, add this code in bold shown in the example below, public function edit() { $output['toolbar_title'] = 'Edit Account';

    // rendering views
    Assets::clear_cache();
    Assets::add_module_js('account','js/custom_edit_ account.js');
    Template::set('data',$output);
    Template::set_view('admin/custom/edit_account');
    Template::render();
}

Here Assets::clear_cache() function clears the assets cache before regenerating the javascript file. This is necessary if you want to use different javascript for different views of the same module. The Assets::add_module_js() function adds the javascript file in the rendered view file (here admin/custom/edit_account') . the first parameter of the add_module_js() function is the module name where the javascript is to be included and second parameter is the path of the js relative to the modules ‘assets’ folder.

5) You can also include module specific css in the same way. Just place the css file in the assets ‘css’ folder in module(/bonfire/modules/account/assets/js/ account.css for example) and call the function Assets::add_module_css() from the controller. public function edit() { $output['toolbar_title'] = 'Edit Account';

    // rendering views
    Assets::clear_cache();
    Assets::add_module_css('account','css/account.css');
    Template::set('data',$output);
    Template::set_view('admin/custom/edit_account');
    Template::render();
}
PeterRock commented 7 years ago

Working with Assets

moxuanyuan commented 7 years ago

@PeterRock 你试试在同一module ,同一controller中,不同方法里加载不同的js,css试试,你就知我说的是什么问题了

vachzar commented 7 years ago

I'm agree with you. sometimes my form need a lot of js for date picker and any other components. But I don't want load it when I just need to show the table of data.

zoliszabo commented 7 years ago

Issue resolved with merged pull request #1278.