Is there a more graceful way to enable/disable fields based on create/edit actions?

Is there a more graceful way to enable/disable fields based on create/edit actions?

chris_nchris_n Posts: 53Questions: 3Answers: 0

Based on information in the forums, I am presently doing this sort of thing:

editFields = ["stock_num", "avg_cost", "uom"];

editor.on( 'initEdit', function () {
    for (var i=0; i<editFields.length; i++) {
        editor.disable(editFields[i]);
    }
} );

editor.on( 'initCreate', function () {
    for (var i=0; i<editFields.length; i++) {
        editor.enable(editFields[i]);
    }
} );

Is there a more graceful way to accomplish this?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin
    edited February 2015 Answer ✓

    You don't need the loop - disable() and enable() will accept arrays:

    editFields = ["stock_num", "avg_cost", "uom"];
    
    editor
      .on( 'initEdit', function () { editor.disable( editFields ); } )
      .on( 'initCreate', function () { editor.enable( editFields ); } );
    

    is about the most graceful way I can think of.

    Allan

  • chris_nchris_n Posts: 53Questions: 3Answers: 0

    Ahh... nice. And chaining++

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    The only other trick I've got to clean it up a bit more is to use arrow functions from ES6:

    editFields = ["stock_num", "avg_cost", "uom"];
     
    editor
      .on( 'initEdit', () => editor.disable( editFields ) )
      .on( 'initCreate', () => editor.enable( editFields ) );
    

    But you would need to use a cross compiler such as Babel JS to have it work in most current browsers...!

    Ultimately perhaps Editor itself should have something like the dependent() method... (thinking on the keyboard... :-) ).

    Allan

  • chris_nchris_n Posts: 53Questions: 3Answers: 0

    Ultimately perhaps Editor itself should have something like the dependent() method... (thinking on the keyboard... :-) ).

    That sounds like a very good idea.

    Chris

This discussion has been closed.