librelist archives

« back to archive

Variable Question

Variable Question

From:
Jason Olson
Date:
2012-10-12 @ 19:43
Hey all,

Sorry I have been AWOL on the hangouts of late.  As a divorced dad, my 
evenings are usually tied up in feeding kids and driving the bus and 
unfortunately my nights on duty are Mondays and Thursdays.  Hope all is 
going well there.

I do have a question about the 'proper' way of doing something.

I am working on a project that involves sending out app requests through 
the Facebook API.  When an invite is sent, a record is entered into the DB
with the user id, name, etc.

My next step was to exclude those already invited on successive requests.
There is a parameter that can be sent of an array of id's to exclude, so 
this was easy to implement.  I am using the Gon gem to pass variables to 
JS.

In my controller

gon.excluded = @invites.collect{ |x| x.recipient_id }

Bam. Worked.

Here was the problem.  I use JQuery to refresh my div's after each request
and don't reload the entire page.  I tried updating the Gon variable in 
the controller and inviting more but the array was not updated.  What I 
found was that the Gon variable is stored in a script using <%= 
include_gon %>.  This was at the top of my application.html.erb.  It 
wasn't being refreshed, of course.

I looked at the gon.watch method, but couldn't get it to work, so what I 
have done is put the <%= include_gon %> tag in a <div> on the invite page,
and added it to the list of divs I update.

I works well, but I am wondering if this is the best practice?

Thoughts?

- JMO

Re: [getarailsjob] Variable Question

From:
David Richards
Date:
2012-10-12 @ 20:24
Hey Jason,

I hear you about the divorced/single dad thing.  When I got my start in
Rails, my wife was just leaving me...you make a lot of crazy tradeoffs when
things like that happen.  So, no worries, whatever you need, we can try to
figure things out with you.

I haven't used the Gon gem before, but looking at it, it looks like a) you
chose a good way to coordinate server and client state and b) you adjusted
well with things.  Specifically, you understood how and when your data was
updating and found a way to adjust.

As far as the gon.watch goes, I'd have to pull it out and try it.  Maybe
Monday?  Even without you there, maybe we can see if we can get that added
to our show_claron Rails app (http://bit.ly/TmlksT) that we're using to
practice Rails and Git skills.  I found the step-by-step on that (
http://bit.ly/PsuqZB), and I think we could do that and come back and
report.

One of the hard parts about rich JavaScript clients is you have to think
harder about communication and two systems' state.  Because this is just
now exploding (even for basic API consumption like for Facebook), there
aren't as many tried-and-true "ways" like you see with Ruby and Rails.
 That is, having someone go before you and fix a few things that you can't
see yet by using their conventions.

So, what's happening now is Rails 4 is coming out with new tools and
conventions tied to client state.  There will be streaming with Rails 4
that should be fairly straightforward to use, plus some of the server-side
caching is going away.  What I'm saying is, I don't feel comfortable yet
with pointing to a convention and thinking it will stick around for the
next year yet.  I do think, however, that we're going to be getting more of
that after we've started building more Rails 4 apps and have more
conversations.

I have been working a lot on similar kinds of things. At first I balked at
Uncle Bob Martin's ideas (http://bit.ly/Q4zLlN) about architecture, but
then I started putting them to use in cradle (http://bit.ly/SXqMIi), and
I'm liking what's happening.  It's like taking three steps back and
thinking very carefully about interfaces, message passing, when and how
messages pass between different systems.  So, this goes a lot farther than
how/whether to use Gon and the best way to setup the asset pipeline.  It
may not change my Rails applications in the short term, but I'm just trying
to make sure I have the kind of clarity you use with figuring out how to
coordinate Ruby and Javascript in two different systems.

Cheers

David


On Fri, Oct 12, 2012 at 1:43 PM, Jason Olson <jmophoto@me.com> wrote:

> Hey all,
>
> Sorry I have been AWOL on the hangouts of late.  As a divorced dad, my
> evenings are usually tied up in feeding kids and driving the bus and
> unfortunately my nights on duty are Mondays and Thursdays.  Hope all is
> going well there.
>
> I do have a question about the 'proper' way of doing something.
>
> I am working on a project that involves sending out app requests through
> the Facebook API.  When an invite is sent, a record is entered into the DB
> with the user id, name, etc.
>
> My next step was to exclude those already invited on successive requests.
>  There is a parameter that can be sent of an array of id's to exclude, so
> this was easy to implement.  I am using the Gon gem to pass variables to JS.
>
> In my controller
>
> gon.excluded = @invites.collect{ |x| x.recipient_id }
>
> Bam. Worked.
>
> Here was the problem.  I use JQuery to refresh my div's after each request
> and don't reload the entire page.  I tried updating the Gon variable in the
> controller and inviting more but the array was not updated.  What I found
> was that the Gon variable is stored in a script using <%= include_gon %>.
>  This was at the top of my application.html.erb.  It wasn't being
> refreshed, of course.
>
> I looked at the gon.watch method, but couldn't get it to work, so what I
> have done is put the <%= include_gon %> tag in a <div> on the invite page,
> and added it to the list of divs I update.
>
> I works well, but I am wondering if this is the best practice?
>
> Thoughts?
>
> - JMO
>