Plotkit with Prototype problem

1 view
Skip to first unread message

Neville

unread,
Mar 29, 2006, 6:22:14 PM3/29/06
to plotkit
Hi,

Thanks for PlotKit, it looks really good.

I'd like to use Plotkit with a Ruby on Rails project, which requires
prototype.js, but there seems to be a problem. The following test page
works just fine without prototype.js, so I'm guessing there is an
incompatibility between prototype and mochikit, but I havent been able
to find the problem - can you help?

Thanks,

Neville

<html>
<head>
<script type="text/javascript"
src="/javascripts/prototype.js"></script>

<script type="text/javascript" src="/mochikit/MochiKit.js"></script>
<script type="text/javascript" src="/plotkit/Base.js"></script>
<script type="text/javascript" src="/plotkit/Layout.js"></script>
<script type="text/javascript" src="/plotkit/Canvas.js"></script>
<script type="text/javascript" src="/plotkit/SweetCanvas.js"></script>

</head>
<body>

<div><canvas id="graph" height="300" width="300"></canvas></div>

<script type="text/javascript">
var layout = new PlotKit.Layout("bar", {});
layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4,
2]]);
layout.evaluate();

var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
plotter.render()
</script>

</body>
</html>

Alastair Tse

unread,
Mar 30, 2006, 6:58:17 AM3/30/06
to plo...@googlegroups.com
I haven't looked much into prototype.js, but would it be possible to
import the prototype stuff after MochiKit? I believe prototype is
confusing MochiKit in the way it modifies the default behaviour of the
default Object object.

Try that and see how you go.

Cheers,

Alastair


--
Alastair Tse
[T] +44 1223 767 017 [M] +44 7795 973639
[W] http://www.liquidx.net/
[ICQ] 113896999 [MSN] liq...@hotmail.com [AIM] alastairtse

Neville

unread,
Mar 30, 2006, 5:31:01 PM3/30/06
to plotkit
I tried adding Prototype after MochiKit but it didnt help.

Is PlotKits interaction with Mochikit reasonably isolated ? Perhaps I
could produce a Prototype version of PlotKit - what do you think ?

Alastair Tse

unread,
Mar 30, 2006, 6:13:38 PM3/30/06
to plo...@googlegroups.com
It depends pretty heavily on the MochiKit stuff, but all the functions
that are used are clearly labelled as MochiKit.*.method. The only real
sticky issue is the use of MochiKit.Color. So if you're brave enough,
you could replace all the functions with their prototype.js
equivalents.

I don't have much experience with prototype.js, so I don't know what
exactly is stopping MochiKit from working, but I believe it is this
reason here:

"It [Prototype.js] makes your code not play well with others, so once
you start using prototype.js, you have to keep using prototype's
paradigm because by extending Array and Object via the prototype
object it secretly modifies some of JavaScripts default behavior."

http://blog.metawrap.com/blog/CommentView,guid,42b961d5-b539-4d9a-b1e0-108e546ae3e6.aspx

It seems a quick google search reveals nothing about anyone having
success in having both prototype.js and MochiKit working together, or
at least maybe its not noteworthy.

I'll have to check it out on the weekend to see what the problem really is.

Cheers,

Alastair

On 3/30/06, Neville <nev...@bmsoft.com.au> wrote:
>

Philippe Marschall

unread,
Apr 1, 2006, 9:48:23 AM4/1/06
to plo...@googlegroups.com
> I'd like to use Plotkit with a Ruby on Rails project, which requires
> prototype.js, but there seems to be a problem.

I can confirm this problem for the Seaside bindings.

The JS exception I get is
'arg has no properties'

It occurs in MochiKit.Base at the marked line below

itemgetter: function (func) {
/***

Returns a function that returns arg[func]

***/
return function (arg) {
return arg[func]; /* at this line here */
};
},

If I remove prototype.js everything works fine.

Cheers
Philippe

Bob Ippolito

unread,
Apr 20, 2006, 9:44:03 PM4/20/06
to plotkit
FYI, MochiKit and Prototype are fully compatible. I occasionally run
through all of MochiKit's unit tests with Prototype loaded just to make
sure that we aren't depending on a pristine JavaScript environment.

Any Prototype related failures w/ PlotKit are due to code in PlotKit.
It's entirely possible to write MochiKit-using code that's not
compatible with Prototype in the same way that it's possible to write
code that's not compatible with Prototype without using MochiKit.

The biggest cause for Prototype incompatibility is the usage of "for (x
in y)". Due to Prototype's gross negligence, extra keys will show up
in that enumeration... even for Arrays. To guard against Prototype
you'd have to either stop using that iteration form, or check the
relevant prototype explicitly or implicitly (e.g. via hasOwnProperty).

Here's an example of what such brain damage looks like, and how it
could be worked around:

>>> Foo = function() {}
function () { }
>>> Foo.prototype.poorlyDesignedLibrariesDoThis = true;
true
>>> f = new Foo()
[object Object]
>>> f.myOnlyProperty = true;
true
>>> keys(f)
["poorlyDesignedLibrariesDoThis", "myOnlyProperty"]
>>> filter(bind('hasOwnProperty', f), keys(f))
["myOnlyProperty"]

-bob

Neville

unread,
May 4, 2006, 8:32:07 PM5/4/06
to plotkit
Hi Alastair,

Bob Ippolito suggests that Plotkit might be changed to work around the
incompatibility with Prototype. I had a quick look at Plotkit's use of
"for (x in y)" but it wasn't clear to me how best to proceed.

Do you have any thoughts as I'd really like to use Plotkit with my Ruby
On Rails project if I can.

Kind Regards

Neville

Alastair Tse

unread,
May 4, 2006, 9:47:41 PM5/4/06
to plo...@googlegroups.com
Neville,

I'm aware of Bob's comments but Prototype compatibility is not the top
of my concerns. I'm happy to accept patches for this, but it means
changing every for(each) loop in the code. If someone is desperate
enough out there, I'm sure they'll come up with the patches ;)

Cheers,

Alastair

Justin Palmer

unread,
May 4, 2006, 10:59:47 PM5/4/06
to plo...@googlegroups.com
I've actually ported 90% of the canvas parts of plotKit to Prototype
which I called plastiK. This required porting some of Mochikit
(primarily the Color parts).

It's still not ready because it has some fragments that need to be
ripped out and I was going to use explorer canvas as well.

I started this port just as a fun experiment to learn more about
canvas and plotKit in general and didn't really have the intentions of
maintaining it on my own because I alone simply can't commit the time
needed to do justice. If you or anyone else is up to helping out in
maintaining it, I am more than willing to help out.

Andrew Dupount is also working on a custom implementation inspired by
plotKit here: http://www.andrewdupont.net/2006/04/23/thought-canvas-ready-for-the-big-time/#respond

In any event, I can upload the current code that I have so others can
play with it.

Cheers,
-Justin Palmer
http://encytemedia.com


--
Justin Palmer
http://encytemedia.com | http://shopify.com | http://jadedpixel.com

Alastair Tse

unread,
May 5, 2006, 4:32:27 AM5/5/06
to plo...@googlegroups.com
Justin,

That's awesome. I would be very interested to take a look at it to see
how much is really needed to make it work with prototype. BTW, if you
want to integrate explorer canvas you can check out the current code
in SVN. The changes are pretty minimal due to the way the code was
structured.

http://projects.liquidx.net/js/browser/plotkit/trunk/PlotKit/Canvas.js

Cheers,

Alastair

Justin Palmer

unread,
May 6, 2006, 1:31:56 AM5/6/06
to plo...@googlegroups.com
Hi Alistair,

I'm almost ashamed to upload this because it's still in the initial
stages (It still has some Mochikit stuff in it in places). I've spent
about 8 hours or so on it.
http://encytemedia.com/treasure/browser/javascript/plastik/

If you're really interested in getting plotKit available for
Prototype, I'd be glad to work with on getting something stable and up
to par with the Mochikit version of plotKit.


The big part of the port was Mochikit's Color implementation and the
collection based stuff (map, collapse, items). Most of the other
stuff was how objects are bound to functions. Wrapping your head
around the code below for the first time was a little rough, but once
I figured out how to translate it to Prototype it got easier. :-)

map(parseFloat, map(getter(0), collapse(map(getter(1), items(this.datasets)))));


Cheers,
-Justin

Bob Ippolito

unread,
May 6, 2006, 1:17:21 PM5/6/06
to plo...@googlegroups.com
Why go through all that trouble when Prototype compatibility could be
had just by rewriting the for loops? Do you really want to maintain a
fork?

-bob

Justin Palmer

unread,
May 6, 2006, 4:10:10 PM5/6/06
to plo...@googlegroups.com
Hi Bob,

It takes more than rewriting the for loops for Prototype compatibility.
There are a number of things that Mochikit has that Prototype doesn't,
and some things are just different all together. Unless you know
something that I don't, I don't see how full Prototype compatibility
could be had by only rewriting the for loops.

As far as maintaining a fork goes, I have no interest in being the
sole maintainer of a Prototype version of plotKit, I wouldn't mind
doing what I could to help maintain it though.

Cheers,
-Justin

Bob Ippolito

unread,
May 6, 2006, 10:25:11 PM5/6/06
to plo...@googlegroups.com
Thanks, but that's not what I said. I said Prototype *compatibility*.
Right now Prototype breaks this and other libraries that use
"for(..in..)" loops because it adds enumerable properties to
Object.prototype. Prototype compatibility can be attained simply by
rewriting these loops.

If Prototype didn't do careless things like this, I wouldn't have had
to write MochiKit in the first place.

Other than size, there's little reason to go rewriting large parts of
PlotKit to replace the MochiKit dependency with something else. I
mean hell, if you're going to do that, you might as well make it
completely standalone.

-bob

Justin Palmer

unread,
May 7, 2006, 1:31:58 AM5/7/06
to plo...@googlegroups.com
Bob,

I'm not here to argue the benefits or practices of Prototype vs.
Mochikit. I think both libraries have their strong points. However,
to address your point about Prototype modifying Object.prototype, this
hasn't been the case for some time now. for...in loops work fine with
Prototype for the Object object. each, and friends were meant to
replace for...in when dealing with Arrays. It's a convention that I,
nor many others have a problem with.

Reply all
Reply to author
Forward
0 new messages