conflict with prototype.js

82 views
Skip to first unread message

Boaz

unread,
Feb 2, 2014, 7:50:04 AM2/2/14
to
I'm using soma (v2.0.4) as an injectable plugin script (I load it into various sites dynamically via a script tag).
I ran into an issue with sites that use the prototype.js library.

Prototype.js defines its own "bind" function, which gets called instead of soma's bind function.

during framework init the mediators object is mapped as a singleton: 
self.injector.mapClass('mediators', Mediators, true);
self.mediators = self.injector.getValue('mediators');

injector.getValue will call createInstance() (line 205):
vo.value = this.createInstance.apply(this, args);

which calls instantiate (line 232), which does this at the end (line 258):
return new (Function.prototype.bind.apply(TargetClass, args))();


this call doesn't go to soma's bind function, but to prototype.js and that's where the problem is.


Romu, would you consider renaming "bind" to a less-common function name?

Romuald Quantin

unread,
Feb 2, 2014, 8:12:36 AM2/2/14
to som...@googlegroups.com
Hi there

I'm afraid I can't do that. Function.prototype.bind is native Javascript.

It has been added in June 2011 (Standard ECMA-262):
http://www.ecma-international.org/publications/standards/Ecma-262.htm

The bind function I'm adding is just the Mozilla shim for older browser:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

Romu

Romuald Quantin

unread,
Feb 2, 2014, 8:20:58 AM2/2/14
to som...@googlegroups.com
This line that instantiates is part of infuse.js that is included in soma.js for the DI.

What could be changed is a way to instantiate a new function with params and the right scope. Even though I never found a way of doing that without Function.prototype.bind.

You could fork infuse.js and try to find an alternative for the syntax, and check that the tests are going through. I can guide you after that to rebuild soma.js with your version of infuse.js, unless I implement it myself if the solution is fine.

Romu

Boaz

unread,
Feb 3, 2014, 4:08:19 AM2/3/14
to som...@googlegroups.com
for now I simply renamed all calls to "bind" with a more unique function name (so all browsers are actually using the shim) and tests look good.
Thanks Romu!

Romuald Quantin

unread,
Feb 3, 2014, 5:43:10 AM2/3/14
to som...@googlegroups.com
This will not solve all your problems, only the ones you encounters already I believe.

As I said, this bind method is native javascript, I don't see why prototype would implement it differently.

You should keep an eye on that, and possibly ask opinion on a prototype board.

Cheers.

Romu

Jason Westbrook

unread,
Feb 3, 2014, 6:24:25 PM2/3/14
to som...@googlegroups.com

As long as you are using a recent version of PrototypeJS (1.7.1) the Function#bind method is only polyfilled in browsers that do not have bind(). Otherwise you are using the native bind() method.

Reply all
Reply to author
Forward
0 new messages