Question

I have a nested form using Cocoon. Within the nested form I have a select menu that I want to call chosen() on.

Normally I would have the following in the code

$('#cust_select').chosen()

However, I need to call this after a nested field has been added using something such as

    $('#container').bind('cocoon:before-insert', function(e, inserted_item) {
      // ... do something
    });

However, I can't get this to work and so have 2 questions

  1. what is the coffee script version of this code?
  2. is inserted_item the actual code to use or should item be replaced with the model name?

any thoughts?

Michael

Was it helpful?

Solution

1) The CoffeeScript version is:

$('#cust_select').chosen() # just the same

$('#container').bind 'cocoon:before-insert', (e, inserted_item) -> 
  # ... do something with the inserted item

2) inserted_item is indeed the inserted item:

From here

To listen to the events, you to have the following code in your javascript:

$('#container').bind('cocoon:before-insert', function(e, inserted_item) {
   // ... do something
});

Where e is the event and the second parameter is the inserted or removed item. This allows you to change markup, or add effects/animations (see example below).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top