0

In Rails, I have a form on my Dish page that has a multi-select Chosen field called Side Dishes.

I'd like to have my Side Dishes Chosen field's options to automatically update (without page reload) if the user added another Side Dish from another browser window.

I looked into the Updating Chosen Dynamically from the Chosen website but I don't know if that's what I'm looking for and/or whether I'm understanding it correctly.

Currently, my dishes.js.coffee looks like this:

jQuery ->
  $(".chzn-side-dish").chosen()
  $(".chzn-side-dish").trigger "liszt:updated"

But clearly that doesn't do the trick. My understanding was that the .trigger will periodically refresh the Side Dish list and pick up on any new additions (or deletions) from the Side Dish table.

And the form on the Dish page follows this convention:

= form_for @dish, :html => { :class => "form-horizontal" } do |f|
...
%fieldset
  .control-group
    =f.label :side_dish_ids, "Side Dish(es)", :class => 'control-label'
    .controls
      =f.collection_select :side_dish_ids, SideDish.order(:name), :id, :name, {}, {multiple: true, :class => 'chzn-side-dish'}

PS -- I've looked at other SO questions but couldn't get it all to work. Again, I'm not sure whether I'm understanding the purpose of the .trigger... properly.

Community
  • 1
  • 1
FilmiHero
  • 2,306
  • 7
  • 31
  • 46
  • jQuery's `.trigger()` is a programmatic way of simulating a browser event that would normally occur in response to a user interaction, eg. 'click' or 'mouseover'. It can also be used with custom events that have no user-interactive equivalent. – Beetroot-Beetroot Jul 18 '12 at 23:16

1 Answers1

1

You will need to handle this manually, there is no simple way. You can either do polling to check if there are changes and then recreate your list, or you could use websockets to avoid needing to poll. There is a service http://www.pusherapp.com that helps with websockets if you take that approach.

I would be very careful from a usability perspective, you don't want to reload the list while a user is actually using it, or maybe if they have already selected something.

Joel Friedlaender
  • 2,191
  • 1
  • 18
  • 24