Yii Framework Forum: Class-based javascript - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Class-based javascript Rate Topic: -----

#1 User is offline   ema 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-June 11

Posted 07 May 2012 - 05:17 PM

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)
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 08 May 2012 - 02:08 AM

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.
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   ema 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-June 11

Posted 08 May 2012 - 11:15 AM

View Postmdomba, on 08 May 2012 - 02:08 AM, said:

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.


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).
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,341
  • Joined: 12-October 09
  • Location:Croatia

Posted 08 May 2012 - 12:26 PM

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...
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   ema 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 11-June 11

Posted 10 May 2012 - 06:59 PM

(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 :))
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users