Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-22 Thread Dmytrii Nagirniak
Hi Michael,

I use the widgets approach.
I've summed it up for you in this gist https://gist.github.com/4133761

Too long to fit in here.

But I basically use data-attributes and JS widgets that work against
those attributes.
Trying to make them easily reusable so we can combine any of them.

There were a few issues with event sequences and cross-browser-ing, but
were eliminate by doing it better.

Cheers.

Regards,
Dmytrii Nagirniak
http://ApproachE.com http://www.ApproachE.com


On 21 November 2012 20:01, Michael Pearson mipear...@gmail.com wrote:

 Hi,

 I'm looking for recommendations as to the best way to create  manage
 buttons that perform an action on an object and then update a portion of
 the page with a response from the server.

 This was relatively simple in Rails 2.3 land: the now removed
 link_to_remote method (
 http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote)
 would automagically generate javascript that would ask your Rails app for a
 snippet and then replace part of the page with that snippet.

 It was, of course, messy as hell, which I think is why it got removed.

 Looking for something that does something similar in 3.x land hasn't
 gotten me very far: the consensus seems to be write your own damn
 javascript. We've done so so far, but it's never been quite as easy as the
 old helper methods.

 Also, I'm haunted by the doing it wrong spectre: the way we're doing it
 is simply aping the way the 2.3 helpers used to work, except with hand
 written UJS rather than generated RJS.

 The example I'm working on right now is a button that, while editing a
 user, allows the administrator to forgive a user's past invoices. The
 button is within an existing form. The code, right now, is bloody terrible:

   = link_to Cancel Outstanding Invoices,
 cancel_outstanding_invoices_user_path(@user), :class = btn btn-danger,
 :id = cancel-outstanding-invoices, :remote = true, :method = :post
   :javascript
 $('#outstanding-invoices').bind('ajax:success',
 function(event, data) { $('#outstanding-invoices').html(data); });

 The Rails action simply performs a render :text =.

 There's a whole bunch of better ways I can think of doing the above - even
 ways that allow me to make the Javascript code completely generic. However,
 I'd rather see how others do it first.

 --
 Michael Pearson


  --
 You received this message because you are subscribed to the Google Groups
 Ruby or Rails Oceania group.
 To post to this group, send email to rails-oceania@googlegroups.com.
 To unsubscribe from this group, send email to
 rails-oceania+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/rails-oceania?hl=en.


-- 
You received this message because you are subscribed to the Google Groups Ruby 
or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-22 Thread Dmytrii Nagirniak
On 21 November 2012 20:01, Michael Pearson mipear...@gmail.com wrote:

 The Rails action simply performs a render :text =.


BTW, better way would be `head :ok`

-- 
You received this message because you are subscribed to the Google Groups Ruby 
or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



[rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Michael Pearson
Hi,

I'm looking for recommendations as to the best way to create  manage
buttons that perform an action on an object and then update a portion of
the page with a response from the server.

This was relatively simple in Rails 2.3 land: the now removed
link_to_remote method (
http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote)
would automagically generate javascript that would ask your Rails app for a
snippet and then replace part of the page with that snippet.

It was, of course, messy as hell, which I think is why it got removed.

Looking for something that does something similar in 3.x land hasn't gotten
me very far: the consensus seems to be write your own damn javascript.
We've done so so far, but it's never been quite as easy as the old helper
methods.

Also, I'm haunted by the doing it wrong spectre: the way we're doing it
is simply aping the way the 2.3 helpers used to work, except with hand
written UJS rather than generated RJS.

The example I'm working on right now is a button that, while editing a
user, allows the administrator to forgive a user's past invoices. The
button is within an existing form. The code, right now, is bloody terrible:

  = link_to Cancel Outstanding Invoices,
cancel_outstanding_invoices_user_path(@user), :class = btn btn-danger,
:id = cancel-outstanding-invoices, :remote = true, :method = :post
  :javascript
$('#outstanding-invoices').bind('ajax:success', function(event,
data) { $('#outstanding-invoices').html(data); });

The Rails action simply performs a render :text =.

There's a whole bunch of better ways I can think of doing the above - even
ways that allow me to make the Javascript code completely generic. However,
I'd rather see how others do it first.

-- 
Michael Pearson

-- 
You received this message because you are subscribed to the Google Groups Ruby 
or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Adam Boas
Hi Michael,

The bad news is that link_to_remote still exists. It has just changed syntax: 
link_to(…, remote: true)

This functionality would be trivial to write in JS or Coffee to attach to the 
button and form and post it via ajax. I definitely wouldn't inline it in the 
link_to though. Write a JQuery plugin and attach it in dom-ready. I would 
probably write it fairly generically, since this form/button/ response 
HTML/text pattern is pretty common. 

Cheers,

Adam Boas
e: adam.b...@gmail.com
m: +61 457 741 117



On 21/11/2012, at 8:01 PM, Michael Pearson mipear...@gmail.com wrote:

 Hi,
 
 I'm looking for recommendations as to the best way to create  manage buttons 
 that perform an action on an object and then update a portion of the page 
 with a response from the server.
 
 This was relatively simple in Rails 2.3 land: the now removed link_to_remote 
 method 
 (http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote) 
 would automagically generate javascript that would ask your Rails app for a 
 snippet and then replace part of the page with that snippet.
 
 It was, of course, messy as hell, which I think is why it got removed.
 
 Looking for something that does something similar in 3.x land hasn't gotten 
 me very far: the consensus seems to be write your own damn javascript. 
 We've done so so far, but it's never been quite as easy as the old helper 
 methods.
 
 Also, I'm haunted by the doing it wrong spectre: the way we're doing it is 
 simply aping the way the 2.3 helpers used to work, except with hand written 
 UJS rather than generated RJS.
 
 The example I'm working on right now is a button that, while editing a user, 
 allows the administrator to forgive a user's past invoices. The button is 
 within an existing form. The code, right now, is bloody terrible:
 
   = link_to Cancel Outstanding Invoices, 
 cancel_outstanding_invoices_user_path(@user), :class = btn btn-danger, :id 
 = cancel-outstanding-invoices, :remote = true, :method = :post
   :javascript
 $('#outstanding-invoices').bind('ajax:success', function(event, 
 data) { $('#outstanding-invoices').html(data); });
 
 The Rails action simply performs a render :text =.
 
 There's a whole bunch of better ways I can think of doing the above - even 
 ways that allow me to make the Javascript code completely generic. However, 
 I'd rather see how others do it first.
 
 -- 
 Michael Pearson
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 Ruby or Rails Oceania group.
 To post to this group, send email to rails-oceania@googlegroups.com.
 To unsubscribe from this group, send email to 
 rails-oceania+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/rails-oceania?hl=en.

-- 
You received this message because you are subscribed to the Google Groups Ruby 
or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Michael Pearson
On Wed, Nov 21, 2012 at 8:10 PM, Adam Boas adam.b...@gmail.com wrote:

 Hi Michael,

 The bad news is that link_to_remote still exists. It has just changed
 syntax: link_to(…, remote: true)

 This functionality would be trivial to write in JS or Coffee to attach to
 the button and form and post it via ajax. I definitely wouldn't inline it
 in the link_to though. Write a JQuery plugin and attach it in dom-ready. I
 would probably write it fairly generically, since this form/button/
 response HTML/text pattern is pretty common.

 You'll see I'm already using the new :remote = true syntax in my example,
which handles part of the problem.

You're right in that re-implementing the lost behaviour would be fairly
trivial - but I'm wondering whether there's a better way to do it?

Returning HTML  text snippets from actions never quite felt right to me,
even if it was fairly easy.

I'm wondering whether we should be looking at something more sophisticated.
I'm worried that by re-implementing the old RJS helpers using UJS will
close us off from better designs for the way our smattering of client side
JS interacts with our Rails app.

-- 
Michael Pearson

-- 
You received this message because you are subscribed to the Google Groups Ruby 
or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Dave Perrett

I'd be interested in hearing how others handle this too.

I usually get everything working initially with regular non-xhr rails 
show/edit/update, then add a mini backbone view on topto hook into 
button events and 'ajaxify' it after the fact:


$(document).ready(function() { new SomeWidgetView().render(); });

... which handles click events, form submits, ajax calls etc. I test the 
backbone views with jasmine, and cucumber can run through the workflow 
with or without @javascript for integration testing. I'm guilty of the 
same render :text = '' if request.xhr? in the controller though in 
the case of POST and PUT, and wonder if there's a better way to handle it?


I also feel a bit dirty doing AJAX GET requests which return HTML, and 
dumping it into the DOM, but going the JSON route and writing an extra 
jscript template to reproduce the same HTML that I've already got in a 
rails view doesn't seem very DRY either.


Any thoughts on AJAX with graceful degradation while remaining DRY?

dave

Michael Pearson wrote:

Hi,

I'm looking for recommendations as to the best way to create  manage 
buttons that perform an action on an object and then update a portion 
of the page with a response from the server.


This was relatively simple in Rails 2.3 land: the now removed 
link_to_remote method 
(http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote) 
would automagically generate javascript that would ask your Rails app 
for a snippet and then replace part of the page with that snippet.


It was, of course, messy as hell, which I think is why it got removed.

Looking for something that does something similar in 3.x land hasn't 
gotten me very far: the consensus seems to be write your own damn 
javascript. We've done so so far, but it's never been quite as easy 
as the old helper methods.


Also, I'm haunted by the doing it wrong spectre: the way we're doing 
it is simply aping the way the 2.3 helpers used to work, except with 
hand written UJS rather than generated RJS.


The example I'm working on right now is a button that, while editing a 
user, allows the administrator to forgive a user's past invoices. The 
button is within an existing form. The code, right now, is bloody 
terrible:


  = link_to Cancel Outstanding Invoices, 
cancel_outstanding_invoices_user_path(@user), :class = btn 
btn-danger, :id = cancel-outstanding-invoices, :remote = true, 
:method = :post

  :javascript
$('#outstanding-invoices').bind('ajax:success', 
function(event, data) { $('#outstanding-invoices').html(data); });


The Rails action simply performs a render :text =.

There's a whole bunch of better ways I can think of doing the above - 
even ways that allow me to make the Javascript code completely 
generic. However, I'd rather see how others do it first.


--
Michael Pearson


--
You received this message because you are subscribed to the Google 
Groups Ruby or Rails Oceania group.

To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.


--
You received this message because you are subscribed to the Google Groups Ruby or 
Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Adam Boas
The real nub is defining what 'better' would mean.
Simplest? Most Re-usable? Easiest to maintain? Least amount of effort?

Its always going to be a trade off. And the 'best' approach is going to be more 
to do with where you app is already at, what the skill set is of the 
developers, and where the app is going in terms of features and functionality.

I tend to lean toward the simplest solution I can manage, particularly on 
pre-existing, large apps. If you already have controllers shipping HTML there 
is nothing wrong with leveraging that and sprinkling a little AJAX pixie dust 
to make the app seem more responsive. To me the only absolute is to keep the 
pixie dust out of the templates and make it clean and readable.

I personally really like Backbone for building an app with significant rich 
client behaviour, but would never introduce it to an existing full page post 
application just to get a little bit of responsive behaviour in some forms. It 
introduces significant complexity that just doesn't make any sense for the kind 
of thing you have mentioned. And if you are not routing or changing views, and 
have no significant model(s) it really adds very little value.

Angular is a framework I have been playing with a bit recently and it does seem 
to offer a nice, lighter weight alternative to just writing Jquery plugins for 
this kind of thing. Its binding behaviour can make responding to your posts 
nice and simple. I can definitely recommend having a bit of a look at if as an 
alternative, particularly if you want to play with the new shiny :-)

Adam Boas
e: adam.b...@gmail.com
m: +61 457 741 117



On 21/11/2012, at 8:41 PM, Michael Pearson mipear...@gmail.com wrote:

 On Wed, Nov 21, 2012 at 8:10 PM, Adam Boas adam.b...@gmail.com wrote:
 Hi Michael,
 
 The bad news is that link_to_remote still exists. It has just changed syntax: 
 link_to(…, remote: true)
 
 This functionality would be trivial to write in JS or Coffee to attach to the 
 button and form and post it via ajax. I definitely wouldn't inline it in the 
 link_to though. Write a JQuery plugin and attach it in dom-ready. I would 
 probably write it fairly generically, since this form/button/ response 
 HTML/text pattern is pretty common. 
 
 You'll see I'm already using the new :remote = true syntax in my example, 
 which handles part of the problem.
 
 You're right in that re-implementing the lost behaviour would be fairly 
 trivial - but I'm wondering whether there's a better way to do it?
 
 Returning HTML  text snippets from actions never quite felt right to me, 
 even if it was fairly easy.
 
 I'm wondering whether we should be looking at something more sophisticated. 
 I'm worried that by re-implementing the old RJS helpers using UJS will close 
 us off from better designs for the way our smattering of client side JS 
 interacts with our Rails app.
 
 -- 
 Michael Pearson
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 Ruby or Rails Oceania group.
 To post to this group, send email to rails-oceania@googlegroups.com.
 To unsubscribe from this group, send email to 
 rails-oceania+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/rails-oceania?hl=en.

-- 
You received this message because you are subscribed to the Google Groups Ruby 
or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Dave Perrett
Interesting, thanks Adam - I've been meaning to have a look at angular. 
This is the kind of thing I've been doing in backbone - 
https://gist.github.com/4124680 . Any thoughts?


Adam Boas wrote:

The real nub is defining what 'better' would mean.
Simplest? Most Re-usable? Easiest to maintain? Least amount of effort?

Its always going to be a trade off. And the 'best' approach is going 
to be more to do with where you app is already at, what the skill set 
is of the developers, and where the app is going in terms of features 
and functionality.


I tend to lean toward the simplest solution I can manage, particularly 
on pre-existing, large apps. If you already have controllers shipping 
HTML there is nothing wrong with leveraging that and sprinkling a 
little AJAX pixie dust to make the app seem more responsive. To me the 
only absolute is to keep the pixie dust out of the templates and make 
it clean and readable.


I personally really like Backbone for building an app with significant 
rich client behaviour, but would never introduce it to an existing 
full page post application just to get a little bit of responsive 
behaviour in some forms. It introduces significant complexity that 
just doesn't make any sense for the kind of thing you have mentioned. 
And if you are not routing or changing views, and have no significant 
model(s) it really adds very little value.


Angular is a framework I have been playing with a bit recently and it 
does seem to offer a nice, lighter weight alternative to just writing 
Jquery plugins for this kind of thing. Its binding behaviour can make 
responding to your posts nice and simple. I can definitely recommend 
having a bit of a look at if as an alternative, particularly if you 
want to play with the new shiny :-)


Adam Boas
e: adam.b...@gmail.com mailto:adam.b...@gmail.com
m: +61 457 741 117



On 21/11/2012, at 8:41 PM, Michael Pearson mipear...@gmail.com 
mailto:mipear...@gmail.com wrote:


On Wed, Nov 21, 2012 at 8:10 PM, Adam Boas adam.b...@gmail.com 
mailto:adam.b...@gmail.com wrote:


Hi Michael,

The bad news is that link_to_remote still exists. It has just
changed syntax: link_to(…, remote: true)

This functionality would be trivial to write in JS or Coffee to
attach to the button and form and post it via ajax. I definitely
wouldn't inline it in the link_to though. Write a JQuery plugin
and attach it in dom-ready. I would probably write it fairly
generically, since this form/button/ response HTML/text pattern
is pretty common.

You'll see I'm already using the new :remote = true syntax in my 
example, which handles part of the problem.


You're right in that re-implementing the lost behaviour would be 
fairly trivial - but I'm wondering whether there's a better way to do it?


Returning HTML  text snippets from actions never quite felt right to 
me, even if it was fairly easy.


I'm wondering whether we should be looking at something more 
sophisticated. I'm worried that by re-implementing the old RJS 
helpers using UJS will close us off from better designs for the way 
our smattering of client side JS interacts with our Rails app.


--
Michael Pearson



--
You received this message because you are subscribed to the Google 
Groups Ruby or Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com 
mailto:rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com 
mailto:rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.


--
You received this message because you are subscribed to the Google 
Groups Ruby or Rails Oceania group.

To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.


--
You received this message because you are subscribed to the Google Groups Ruby or 
Rails Oceania group.
To post to this group, send email to rails-oceania@googlegroups.com.
To unsubscribe from this group, send email to 
rails-oceania+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.



Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Ben Taylor
Something I use a lot is AJAX'd modals. So I have a little bit of JS that looks 
like:  

$ -
  $(document).on 'click', '[data-load-modal]', -
url = $(this).attr('data-load-modal')
Modal.open(url)


  $(document).on 'click', '[data-close-modal]', Modal.close  

window.Modal =  
  close: -
$overlay.addClass hidden
$modal.addClass hidden

  open: (url) -
$overlay.removeClass hidden

extras =  
  modal: true

 # this should be done by a URL parser
if /\?/.exec(url)
  for pair in url.split('?')[1].split('')
a = pair.split(=)[0]
b = pair.split(=)[1]
extras[a] = b

$.ajax
  url: url
  data: extras
  dataType: 'html'
  success: (result) = $modal.html(result)
  error: (xhr, string, errorThrown) -
  direError There was an error contacting the server


You could potentially follow this pattern and do something like:

%= link_to More, articles_path(offset: @offset + @step), :remote = true, 
data-append-to = #articles %

$(document).on ajax:success, [data-append-to], (event, result) -
  selector = $(event.target).attr(data-append-to)
  $(selector).append(result)

Then add others, like data-replace and data-prepend-to. This way you don't have 
to write a lot of JS and get something similar to the 2.3 behaviour.

 - Ben


On Wednesday, 21 November 2012 at 11:54 PM, Dave Perrett wrote:

 Interesting, thanks Adam - I've been meaning to have a look at angular. This 
 is the kind of thing I've been doing in backbone - 
 https://gist.github.com/4124680 . Any thoughts?  
  
 Adam Boas wrote:  
  The real nub is defining what 'better' would mean.  
  Simplest? Most Re-usable? Easiest to maintain? Least amount of effort?
   
  Its always going to be a trade off. And the 'best' approach is going to be 
  more to do with where you app is already at, what the skill set is of the 
  developers, and where the app is going in terms of features and 
  functionality.  
   
  I tend to lean toward the simplest solution I can manage, particularly on 
  pre-existing, large apps. If you already have controllers shipping HTML 
  there is nothing wrong with leveraging that and sprinkling a little AJAX 
  pixie dust to make the app seem more responsive. To me the only absolute is 
  to keep the pixie dust out of the templates and make it clean and readable. 
   
   
  I personally really like Backbone for building an app with significant rich 
  client behaviour, but would never introduce it to an existing full page 
  post application just to get a little bit of responsive behaviour in some 
  forms. It introduces significant complexity that just doesn't make any 
  sense for the kind of thing you have mentioned. And if you are not routing 
  or changing views, and have no significant model(s) it really adds very 
  little value.  
   
  Angular is a framework I have been playing with a bit recently and it does 
  seem to offer a nice, lighter weight alternative to just writing Jquery 
  plugins for this kind of thing. Its binding behaviour can make responding 
  to your posts nice and simple. I can definitely recommend having a bit of a 
  look at if as an alternative, particularly if you want to play with the new 
  shiny :-)  
   
  Adam Boas
  e: adam.b...@gmail.com (mailto:adam.b...@gmail.com)
  m: +61 457 741 117
   
   
   
  On 21/11/2012, at 8:41 PM, Michael Pearson mipear...@gmail.com 
  (mailto:mipear...@gmail.com) wrote:
   On Wed, Nov 21, 2012 at 8:10 PM, Adam Boas adam.b...@gmail.com 
   (mailto:adam.b...@gmail.com) wrote:
Hi Michael,
 
The bad news is that link_to_remote still exists. It has just changed 
syntax: link_to(…, remote: true)
 
This functionality would be trivial to write in JS or Coffee to attach 
to the button and form and post it via ajax. I definitely wouldn't 
inline it in the link_to though. Write a JQuery plugin and attach it in 
dom-ready. I would probably write it fairly generically, since this 
form/button/ response HTML/text pattern is pretty common.   
 
   You'll see I'm already using the new :remote = true syntax in my 
   example, which handles part of the problem.

   You're right in that re-implementing the lost behaviour would be fairly 
   trivial - but I'm wondering whether there's a better way to do it?  

   Returning HTML  text snippets from actions never quite felt right to me, 
   even if it was fairly easy.

   I'm wondering whether we should be looking at something more 
   sophisticated. I'm worried that by re-implementing the old RJS helpers 
   using UJS will close us off from better designs for the way our 
   smattering of client side JS interacts with our Rails app.  

   -- Michael Pearson



   --  
   You received this message because you are subscribed to the Google Groups 
   Ruby or Rails Oceania group.
   To post to this group, send email to rails-oceania@googlegroups.com 
   

Re: [rails-oceania] Recommendations for AJAX buttons actions in Rails 3.2

2012-11-21 Thread Adam Boas
Hi Dave,

That is a nice, clean Backbone view and if I already had Backbone in the mix 
and had a page with multiple Ajax posts it would be a fairly neat solution. I 
don't think there is anything wrong with going that way, I just generally try 
to keep the stack as simple as possible for as long as possible. Generally that 
means writing a simple Jquery plugin for posting forms and dealing with the 
HTML fragment or JSON returns. You'll notice that your View is really just a 
wafer thin wrapper to the Jquery $.ajax call, doing a little coordinating. It 
is not really using any of the actual backbone functionality (besides events), 
just borrowing its style and View class.

I generally bring in Backbone when I have some heavier lifting to do and/or I 
see that my Javascript is manifesting a confusion of concerns. Backbone can 
then give me a structure that helps, generally I don't feel I get that much 
value out of Backbone unless I have a proper JSON based API to leverage and at 
least one domain class.

As I mentioned earlier, I have been experimenting, recently with using Angular 
for these 'halfway house' problems, mostly because I can get value from its 
data binding and scoping even for reasonably trivial things. I'm still not 
totally convinced that the kind of problem that Michael mentioned requires even 
that but it certainly could be helpful.

Cheers,

Adam Boas
e: adam.b...@gmail.com
m: +61 457 741 117



On 21/11/2012, at 11:54 PM, Dave Perrett perrett.d...@gmail.com wrote:

 Interesting, thanks Adam - I've been meaning to have a look at angular. This 
 is the kind of thing I've been doing in backbone - 
 https://gist.github.com/4124680 . Any thoughts? 
 
 Adam Boas wrote:
 
 The real nub is defining what 'better' would mean.
 Simplest? Most Re-usable? Easiest to maintain? Least amount of effort?
 
 Its always going to be a trade off. And the 'best' approach is going to be 
 more to do with where you app is already at, what the skill set is of the 
 developers, and where the app is going in terms of features and 
 functionality.
 
 I tend to lean toward the simplest solution I can manage, particularly on 
 pre-existing, large apps. If you already have controllers shipping HTML 
 there is nothing wrong with leveraging that and sprinkling a little AJAX 
 pixie dust to make the app seem more responsive. To me the only absolute is 
 to keep the pixie dust out of the templates and make it clean and readable.
 
 I personally really like Backbone for building an app with significant rich 
 client behaviour, but would never introduce it to an existing full page post 
 application just to get a little bit of responsive behaviour in some forms. 
 It introduces significant complexity that just doesn't make any sense for 
 the kind of thing you have mentioned. And if you are not routing or changing 
 views, and have no significant model(s) it really adds very little value.
 
 Angular is a framework I have been playing with a bit recently and it does 
 seem to offer a nice, lighter weight alternative to just writing Jquery 
 plugins for this kind of thing. Its binding behaviour can make responding to 
 your posts nice and simple. I can definitely recommend having a bit of a 
 look at if as an alternative, particularly if you want to play with the new 
 shiny :-)
 
 Adam Boas
 e: adam.b...@gmail.com
 m: +61 457 741 117
 
 
 
 On 21/11/2012, at 8:41 PM, Michael Pearson mipear...@gmail.com wrote:
 
 On Wed, Nov 21, 2012 at 8:10 PM, Adam Boas adam.b...@gmail.com wrote:
 Hi Michael,
 
 The bad news is that link_to_remote still exists. It has just changed 
 syntax: link_to(…, remote: true)
 
 This functionality would be trivial to write in JS or Coffee to attach to 
 the button and form and post it via ajax. I definitely wouldn't inline it 
 in the link_to though. Write a JQuery plugin and attach it in dom-ready. I 
 would probably write it fairly generically, since this form/button/ 
 response HTML/text pattern is pretty common. 
 
 You'll see I'm already using the new :remote = true syntax in my example, 
 which handles part of the problem.
 
 You're right in that re-implementing the lost behaviour would be fairly 
 trivial - but I'm wondering whether there's a better way to do it?
 
 Returning HTML  text snippets from actions never quite felt right to me, 
 even if it was fairly easy.
 
 I'm wondering whether we should be looking at something more sophisticated. 
 I'm worried that by re-implementing the old RJS helpers using UJS will 
 close us off from better designs for the way our smattering of client side 
 JS interacts with our Rails app.
 
 -- 
 Michael Pearson
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 Ruby or Rails Oceania group.
 To post to this group, send email to rails-oceania@googlegroups.com.
 To unsubscribe from this group, send email to 
 rails-oceania+unsubscr...@googlegroups.com.
 For more options, visit this group at