Page 1 of 1

WYMeditor creating new instances every time

PostPosted: Sat Aug 01, 2009 11:32 pm
by rwt
Hello all.
I've been in a deep pit for hours here.
I'm using ajax to create a form with two WYMeditor boxes. Then ajax to send the form. If theres an error, the form is displayed again. fairly simple
Now here's my problem. Whenever the form is created again, WYMeditor creates a new instance. I tried deleting the instances beforehand, updating, etc… nothing works.
First I thought the update wasn't working. Because when I submitted the page, it worked the first time, then kept the same values the next times.
Then looking at the DOM with Firebug, I saw that the iframe WYMeditor instances where actually adding up. 1,2,3,4,5,6,7 etc…
What a pain!!
Why?? I replace the content of the parent div with ajax, so why?

Code: Select all
   var wym1 = jQuery.wymeditors(0);
   var wym2 = jQuery.wymeditors(1);
   var fid = $(this).attr('title');
   var fcat = $(this).siblings('select#catid').children('option:selected').attr('value');
   var fpar = $(this).siblings('#parent').val();
      type: "POST",
      url: "ajax.php",
      cache: "no-cache",
      data: ({
         ed : fid,
         edcat : fcat,
         edpar : fpar,
         edshort : wym1.html(),
         edlong : wym2.html(),
      beforeSend: function(){
         jQuery.each(WYMeditor.INSTANCES, function() {
            delete this;
      success: function(html){
         $('#box fieldset').html(html);

Is it normal? How do I prevent it? Please help.


Re: WYMeditor creating new instances every time

PostPosted: Tue Aug 04, 2009 1:37 pm
by rwt
bump ?

Re: WYMeditor creating new instances every time

PostPosted: Tue Aug 04, 2009 10:31 pm
by rwt
wohoo!! I found my answer… so freaking simple!!!

After havin loaded my ajax, I execute this line:

Code: Select all
WYMeditor.INSTANCES = WYMeditor.INSTANCES.slice(3);

It works because I know how many INSTANCES I have. Otherwise you would need to save the number in a var like this:
Code: Select all
var wymlenght = WYMeditor.INSTANCES.lenght;

Then add after ajax call:
Code: Select all
WYMeditor.INSTANCES = WYMeditor.INSTANCES.slice(wymlenght);

Then EVERYTHING (and I mean everything) worked!! yee-haa!
Note: Don't forget to update the editors beforehand.

Pour les francophones qui captent vraiment pas l'anglais, et qui auraient un problème d'accumulation d'instances, utilisez la ligne de code cité au dessus.

Re: WYMeditor creating new instances every time

PostPosted: Mon Aug 17, 2009 9:49 am
by Nico

Your solution is strange.

You have an array. You get the length of this array, and than you slice this array, starting after the end of the array.
So of course you'll always get an empty array. This is how the "slice" method works.

Why not simply create a new array, the result is exactly the same, and it's really simpler, no ?

so you should just do :

Code: Select all
WYMeditor.INSTANCES = [];

And that's all, it should work exactly the same.