Posted 07 May 2012 - 05:17 PM
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)
Posted 08 May 2012 - 11:15 AM
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).
Posted 08 May 2012 - 12:26 PM
Posted 10 May 2012 - 06:59 PM
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