Ruby on Rails Thursday, May 29, 2014

Pierre-Andre M. wrote in post #1147586:
> I have a variable created by some ruby in my controller that looks like
> this:
>
> @begpoint = row["begpoint"]
>
>
> and I want to pass it into some js that is referenced from within my
> view:

There are several way of doing this, but the simplest is to use HTML5
data- attributes.

Example:

HTML:

<body data-begpoint="<%= @begpoint %>">
... page content
</body>

jQuery:

var begpoint;

$(funtion() {
begpoint = $('body').data('begpoint');
});

So to think of is as "pass it to JavaScript" is sort of inside out. You
use JavaScript to "get" the value from the DOM. jQuery gives you nice
syntax for doing just that.

This is good for relatively small amount of data. If you need lots of
data in your JavaScript there are alternatives to this technique. A good
place to learn something about those techniques is something like
Backbone.js, Ember.js or Angular.

http://backbonejs.org
http://emberjs.com
https://angularjs.org

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/714ade55ebdc879859a21aff99b0a140%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment