Jump to content

ModuleJS and js autoload


Spica
 Share

Recommended Posts

I wonder about behavior of module as this:

<?php

class JqueryFooTable extends ModuleJS { 

	public static function getModuleInfo() {
		return array(
			'title' => 'JqueryFooTable',
			'version' => 1,
			'summary' => 'jQuery plugin to make HTML tables responsive',
			'href' => 'http://fooplugins.com/plugins/footable-jquery/',
			'icon' => 'smile-o',
			'singular' => true,
			'autoload' => false,
			);
	}

	public function init() {
		$this->config->scripts->append($this->config->urls->siteModules . "JqueryFooTable/footable.js");
		$this->config->scripts->append($this->config->urls->siteModules . "JqueryFooTable/footable.filter.js");
	}

}

I'd expected JqueryFooTable.js to be autoloaded. But with the init function it doesnt, only without init.

Is this a proper way to additionaly load js plugins?

Link to comment
Share on other sites

The ModuleJS::init() takes care of loading those scripts if they have the same name as your module, so you don't need to overwrite this method.

Edit: If you need to append additional JS/CSS, call parent::init() first

  • Like 4
Link to comment
Share on other sites

I don't think it is always that simple - it depends on what your module extends. In your case you are extending ModuleJS so you'll be fine with Wanze's solution, but just wanted to mention it.

From Ryan (https://processwire.com/talk/topic/1416-inputfieldmodule-doesnt-autoload-their-jscss/?p=12789)

Any modules that extend: Inputfield, Process or ModuleJS will auto-load their CSS/JS files if they have the same name as the module and appear in the same directory. However, in order for that to work, their init() method has to be called. So if your module extends one of those, and has an init() method, then make sure to call the parent init() method:

You should also read this blog post by soma:

http://soma.urlich.ch/posts/custom-js-in-processwire-admin/

  • Like 2
Link to comment
Share on other sites

Adding a parent::init(); solved it.

<?php

class JqueryFooTable extends ModuleJS { 

	public static function getModuleInfo() {
		return array(
			'title' => 'JqueryFooTable',
			'version' => 1,
			'summary' => 'jQuery plugin to make HTML tables responsive',
			'href' => 'http://fooplugins.com/plugins/footable-jquery/',
			'icon' => 'smile-o',
			'singular' => true,
			'autoload' => false,
			);
	}

	public function init() {
		parent::init();
		$this->config->scripts->append($this->config->urls->siteModules . "JqueryFooTable/footable.js");
		$this->config->scripts->append($this->config->urls->siteModules . "JqueryFooTable/footable.filter.js");
	}

}
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...