New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding support for AMD with RequireJS #56
adding support for AMD with RequireJS #56
Conversation
Among all the changes in the refactoring for testability branch (https://github.com/PMSI-AlignAlytics/dimple/tree/refactoring-for-testability) I have moved each method into its own closure and built the objects using a prototype model. It's a much nicer approach and means that each js file is well formed. Would that make it cleaner to implement this? If so we should make that change to the master branch, the refactoring branch is massive and won't be getting merged for a while. |
Sure, I'll take a look and see how it would fit in with the new structure - also just trying to get some tests going and have it play nicely with karma... Will shout when done! |
For those of us who are struggling using shims... how do you get shims to work for dimple.js exactly? You can now answer this on stackoverflow and get a few points! |
Hi @davclark, A bit of background from the RequireJS site
Ok, now to jump in to the problem... To create a RequireJS shim for dimple has become a bit more complicated recently because recent versions of d3 (as you know, a dependency of dimple) hides itself from the global namespace (which is a good practice) if it detects that RequireJS/AMD is being used. Now currently dimple relies on browser globals (for example it expects d3 to be registered in the global namespace) and it also registers itself in the global namespace too. So the first part of using dimple with RequireJS is to make sure you reference a version which supports AMD. At the time that I'm writing this, no version on the original repo does, but you can modify your dimple.js and add the following to it :
As per the pull request you're commenting on. Save the modified dimple.js file with the suffix ".amd" Great now thats out of the way, d3 will be available in the global namespace and dimple will be defined so we can go ahead and make our shim. In your requirejs config, add paths to your AMD modified version of dimple and the normal d3 script. And add a shim to specify that dimple depends on d3 :
Thats it, should be all there is to it. You can now write modules that require dimple and everything should load up nicely. Yes the global namespace has dimple and d3 in it, but thats not the end of the world. (imho) although we are thinking of a way to structure dimple to be fully AMD compatible, it's going to have quite an impact on the grunt build of the project and how things are structured so it's unlikely it will be available soon. In the meantime, this work-around will let you use dimple with RequireJS. You can find out more about RequireJS shims from the RequireJS site, they'll probably do a whole lot better at explaining this than I did! |
That was very helpful - I wasn't thinking of a shim as being a library, I was just thinking of it as a config call. I did read the docs on the RequireJS site, and your explanation added a lot to my understanding. Thank you! |
Glad it helped! How did you get on in the end? Did you get to a solution? I saw from your stackoverflow question you're wanting to use it with IPython notebook - I'm really interested in that so if you get a chance reply on that stack thread and we can take a look! On Thu, Mar 6, 2014 at 11:44 PM, Dav Clark notifications@github.com
|
I followed the instructions for creating a dimple.amd specified by @stephen-james and embedded dimplejs (based on @davclark's example) into an IPython notebook: http://nbviewer.ipython.org/gist/anonymous/9501035. The dimple graphs show up, but the hover boxes are empty. I don't know whether it's some interaction with ipynb css with the dimple css that's the problem. |
I think the empty hovers are related to an issue with dimple's tooltip class clashing with bootstrap, but John will be able to advise better for this one! Great to hear you got it working in IPython notebook, I'll take a look - am sure some of the guys here will find that really useful On Wed, Mar 12, 2014 at 10:55 PM, Raymond Yee notifications@github.com
|
Yes, just add the following to your CSS:
It's a clash with bootstrap as Stephen says. More detail here: #34 |
Thanks so much -- the CSS given by @johnkiernander did the trick: |
I learned when showing the notebook to my students that the demonstration works with the current IPython 2.0 beta but not IPython 1.2.1. So back to the drawing board to figure out the reasons why. |
While it may not be worth it now that 2.0 is released, @minrk can likely provide the explanation. |
Stephen, I decided against the code refactor in v2.0 in the end due to the problems it would cause with adding requirejs support. Can you try this out with dimple v2.0, if it still works which I'm sure it will, let me know and I'll merge it for the next release. |
Sure I'll give it a go this evening |
…plates modified to allow for dynamic versioning and script inclusion via Gruntfile
…ng Dimple with RequireJS
Using RequireJS to define modules that require dimple is complicated by the fact that d3 doesn't register itself in the global namespace if it detects AMD/RequireJS.
So while it is possible to "shim" dimple using RequireJS, the d3 global reference that dimple relies on is missing (even though the d3 module is loaded).
One approach to solving this would be to wrap the entire dimple creation code in a function that serves d3 to dimple via closure. (below example code untested!)
... but due to wrapping everything in a second level function (
factory
) all script files would have to be indented further, or we'd have to modify the grunt task to do that indentation on build. I'm also not convinced that this is most efficient.The option presented in this pull request is a load simpler, it retrofits d3 in the global namespace if RequireJS is detected (perhaps bad for namespace pollution, but negligably?) and registers dimple both in the global namespace and as a module.
the answer to the following stack overflow question might help weigh this up....
http://stackoverflow.com/questions/17936854/when-designing-a-js-library-should-i-make-it-requirejs-amd-compatible-or-not