Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

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

Open
moxuanyuan opened this issue Apr 28, 2017 · 4 comments
Open

Comments

@moxuanyuan
Copy link

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/ 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.

  1. 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
Copy link
Contributor

Working with Assets

@moxuanyuan
Copy link
Author

moxuanyuan commented Apr 28, 2017

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

@vachzar
Copy link
Contributor

vachzar commented May 3, 2017

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
Copy link

Issue resolved with merged pull request #1278.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants