yii-jquery icon indicating copy to clipboard operation
yii-jquery copied to clipboard

More customizable initialization in yii.js

Open arogachev opened this issue 8 years ago • 3 comments

I found this issue during writing tests for yii.js.

Currently the yii.js is initialized right when the DOM is ready and applies all functionality:

jQuery(function () {
    yii.initModule(yii);
});

and:

init: function () {
    initCsrfHandler();
    initRedirectHandler();
    initScriptFilter();
    initDataMethods();
}

First of all, the user might not need all provided functionality. Let's say he doesn't need "data methods" functionality or "script filter" functionality, but still needs to automatically add CSRF or perform redirects after AJAX.

There is no way to configure that, we either get all or nothing.

Also there is no "destroy" functionality (because it's not a jQuery plugin I guess), so the only way to deactivate something is to deeply investigate the code and understand what it does and then deactivate proper event handlers, etc. This is not user-friendly at all. And I'm not sure if it's possible to deactivate code registered with $.ajaxPrefilter (unless it's done in the callback itself under certain conditions).

So the proposal is to add more flexible initialization and destroy functionality to temporarily deactivate something and initialize again.

arogachev avatar Dec 30 '16 10:12 arogachev

How would you do it? That's BC breaking for sure, right?

samdark avatar Dec 30 '16 14:12 samdark

@samdark At least we can have properties like enableCsrfHandler, enableRedirectHandler and so on, then wrap these init function calls with if (pub.enableSomeFeature) { } kind of checks and give them default true values to prevent BC break. Hovewer setting them can be a problem, see #13295. I thought about passing them through data attributes or decoupling this part from yii.js:

jQuery(function () {
    yii.initModule(yii);
});

and initialize it along with other widgets maybe.

As for destroy part and other nuances, it requires more deeper thinking. But in case of some radical changes we can always defer this issue to 2.1.

arogachev avatar Jan 01 '17 08:01 arogachev

Yes, using data attributes sounds great.

samdark avatar Jan 02 '17 13:01 samdark