index/index.phtml - Add js variable into a namespace ? #21

Open
jcfr opened this Issue Feb 20, 2012 · 3 comments

Comments

Projects
None yet
2 participants
Contributor

jcfr commented Feb 20, 2012

@zachmullen and @cpatrick: Is adding JS code to a div the recommended / standard midas way to expose data to JS ?

Using jsonContent div may conflict with some other code, what about using an explicit namespace ?
For example: https://github.com/midasplatform/slicerpackages/blob/master/controllers/ViewController.php#L267

Open question: What about using the api proposed by jquery? See http://api.jquery.com/data/

Owner

zachmullen commented Apr 16, 2012

To answer your question, yes, this is a commonly used way to pass such data. The javascript is written separately from the views, and we do not populate any of our javascript from the controllers. The controllers can only pass data to the views, therefore we have to go through the view to pass data to our javascript. This is a good thing: it makes our javascript code static so that browsers can cache it; only the view code has to be dynamic. An alternative might be to never pass json through the view, but to force every view (via js) to re-request the server to get its json, but this would that would be expensive, redundant, and break atomicity. Either way, we have to get a serialized json object and then parse it into a hierarchy, so we might as well do it via the same request. Storing it inside a DOM element is just a mechanism for naming the data essentially.

The data() function of jQuery is used to store arbitrary data on DOM nodes. We use it in many places, but it is unrelated to the problem of passing data from the view into our javascript context, since you can only use it if you already have the data in your javascript context. If you have that, then the problem is already solved :)

You're right, we should probably namespace both the div and the globally scoped variable that we use to store our passed json. ("#midasJsonContent" and "var midas.json").

Owner

zachmullen commented Apr 16, 2012

Note that renaming json to midas.json would be another significant javascript refactor, as we reference it in many many scripts in core as well as modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment