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

Running code when array element is instanced #583

Open
punto- opened this issue Dec 13, 2017 · 9 comments
Open

Running code when array element is instanced #583

punto- opened this issue Dec 13, 2017 · 9 comments

Comments

@punto-
Copy link

punto- commented Dec 13, 2017

Hi.

I have a form with some arrays, and I need to run some code when a new element of the array is instanced (by pressing the "Add New Item" button). I put a postRender callback on the options for the array (it's inside the "items" structure), and the function runs when I press the new item button, but the element is never instanced. What's the proper way to do this? Do I need to return something from the function to approve the instancing? (doesn't seem likely since postRender would happen after it's been created).

Thanks!

@ambischof
Copy link
Contributor

Check what the arguments are in the postRender function. There may be a callback function as the first argument which needs to be called to trigger subsequent setup functions.

e.g.:

postRender: function(callback){
  //Do stuff

  callback();
}

@punto-
Copy link
Author

punto- commented Dec 14, 2017 via email

@ambischof
Copy link
Contributor

I think they are different depending, and I don't know that it is in the docs. If you post the code or better yet, make a jsfiddle, I might be able to help.

@punto-
Copy link
Author

punto- commented Dec 14, 2017 via email

@WhileTrueEndWhile
Copy link

WhileTrueEndWhile commented Dec 15, 2017

The postRender callback does not belong in the options. It is located at the top level next to schema and options. Example:

{
    "schema": ...,
    "options": ...,
    "postRender": function(control) {
        // See 'children' and 'childrenByPropertyId' to get other controls:
        console.log(control);
    }
}

Update: Function signature and parameter usage have been corrected. See http://alpacajs.org/docs/api/events.html

@punto-
Copy link
Author

punto- commented Dec 21, 2017

So then is there a way to run code when a new array element is instanced? I tried the "change" event, but it doesn't fire when the array length changes, only when one of the inputs inside the elements change. Thanks.

@WhileTrueEndWhile
Copy link

Of course "postRender" doesn't help here... You could try to replace the event "change" with "add"....

@punto-
Copy link
Author

punto- commented Dec 22, 2017

It looks like "add" doesn't work either.. I made a test here https://pastebin.com/pWgr208n , change runs when a value inside the array element changes, but "add" and "remove" never run

@WhileTrueEndWhile
Copy link

The components for array and table do not seem to work correctly in this case. The events are not fired when an element is added to the array and it is even worse with the table, because the first time you add an element, the event remove is fired and nothing after that. The only chance I see is a bugfix... Or, since it is only the missing or incorrect firing of an event, you could overwrite the options.view.templates.container-array-toolbar property with a custom template (e.g. as simple JSON string) that adds an inline click event handler to the buttons. To do this, you should enhance the following template: https://github.com/gitana/alpaca/blob/master/src/templates/web-edit/container-array-toolbar.html.

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

3 participants