0

Has anyone experienced that only part of their codes that gets ignored by Optimizely?

So my development environment used npm and injector on chrome. So far my code works pretty. But when I copy-pasted the built version to Optimizely custom JS, only certain parts of them that gets ignored. Specifically this part, which makes me think if there's code restrictions on Optimizely, aside of it's not supporting ES6.

// rebind events to the new cartactivator
$.each($._data($OldCartActivator[0], "events"), function () {
    // iterate registered handler of original
    $.each(this, function () {
        $CartActivator.bind(this.type, this.handler);
    });
});

I'd say only this part, coz the rest of the codes are executing pretty well. I can't understand why.

Please enlighten me.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98

1 Answers1

1

I managed to get it work! It's apparently timing issue where the code above executed before there's any events bound the old DOM. So I used waitUntil to uh, wait... until the oldcartactivator has actually any events bound to it before attempting to replicate.

  utils.waitUntil(function(){
    return $._data($OldCartActivator[0], "events")!==undefined && $CartActivator ;
  }).then(function(){
    try{
                $.each($._data($OldCartActivator[0], "events"), function () {
              // iterate registered handler of original
              $.each(this, function () {
                $CartActivator.bind(this.type, this.handler);
              });
            }); 
    }catch(err){}

  });

Poll would also work if you are expecting more than one events that would be attached much later after, but within limited time.

Hope this helps someone in same boat. Cheers~