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/ custom_edit_ 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();
}