Skip to content
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

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

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

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

jcfr opened this issue Feb 20, 2012 · 3 comments

Comments

@jcfr
Copy link
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/

@zachmullen
Copy link
Member

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").

@zachmullen
Copy link
Member

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
Labels
None yet
Development

No branches or pull requests

2 participants