Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array Item Listener #343

Open
hugsbrugs opened this issue Feb 16, 2015 · 1 comment
Open

Add Array Item Listener #343

hugsbrugs opened this issue Feb 16, 2015 · 1 comment

Comments

@hugsbrugs
Copy link

Hi !

I would like to know how I can have a listener when a user clicks on "+Item" when adding Array (or Object) Item.

I can listen to : editor.watch('root.MY_ARRAY',function() but this listen indifferently to an existing Item modification or a new Item. And as I need to perform some specific actions on item creation, I would like to be able to differentiate them !

Thank for highlighting me !

@jdorn
Copy link
Owner

jdorn commented Feb 22, 2015

There's no built in way to listen to add events, but you can do something like this:

var previous_array_length = 0;

editor.on('change',function() {
  // Get your array value
  var arr = editor.getValue().MY_ARRAY;

  // If array has grown in size
  if(arr.length > previous_array_length) {
    // do something
  }

  // Update current array length
  previous_array_length = arr.length;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants