Class-based javascript

(Emanuele Cesena) #1

One of the things I don’t like so much in Yii 1.1 is the way js is attached to object by means of ids.

The scenario I have in mind is a page with multiple instances of “objects with a script”, e.g. a CListView where the _view contains an ajaxLink. Attaching the onclick function to each link via it’s id (i.e. “$(’#idX’).click(…)”), has the following disadvantages:

  1. makes the code longer

  2. invalidates the functions after ajax update (actually it may make them really inconsistent)

What I usually do is to:

  • avoid ajaxLink and put link, adding a class "ajax"

  • create a function similar to the yii one, and attach it to the class, i.e. $(‘a.ajax’).click(…)

  • refresh such function after ajax update

I’m not sure this is the best way to do, but I couldn’t find a better solution… Moreover as a “proof” of my “theory”, I noticed that most of the extensions I’ve tried actually fail to work when either you put two instances in the same page and/or you attempt to use them after an ajax update.

I think that what is missing (and could be cool for Yii2) is a consistent way to:

  • register scripts that need to be refreshed after ajax update (e.g. a new CClientSide::POSITION_AJAXUPDATE)

  • attach these scripts to a class, instead of to each id.

  • refresh all the registered scripts (e.g. a Yii-global function or event for that)

(Maurizio Domba Cerin) #2

You are on a good path :D

Depending on your JS code… you can use the jQuery on() method, this way you don’t need to refresh your custom script at all after the ajax update.

(Emanuele Cesena) #3

Yes and no. Most of the code (the ajaxLink for instance) can be done with on(). Still yii now attaches code to the ids, you have to manually attach a similar code with on() for the ajax update, and as a result there is double code (yii attaching code to the class would be better).

However other code requires to re-initialize the objects after ajax update, for instance twipsy or fgmenu (for all there are yii extensions).

In my previous post I really had in mind this kind of code… I notice now that I oversimplified, sorry :)

Another option is to fetch js code from ajax, but I don’t really like such a solution (I’ve a background on security, I couldn’t live with something like that).

(Maurizio Domba Cerin) #4

I did understood you completely but did not explain in details… ajaxlink() is just a helper for simple situations… .for more complex situations like the one you are describing ajaxlink (and all other ajax-ed methods) are not good… so you need to use the standard link method with a custom JS code like I explained above…

(Emanuele Cesena) #5

(sorry for the delay)

Yes, my proposal in fact was to extend yii with the possibility to express class-based js. If you think to listViews, this is a use case that happens quite frequently in my opinion. At least if would be an interesting feature for yii2 (unfortunately I can’t write in the yii2 forum :))