Delete dependent event.

Delete dependent event.

kaustubh.agrawal2000kaustubh.agrawal2000 Posts: 88Questions: 39Answers: 2

Is it possible to delete a dependent event before adding one ??
OR
is it possible to execute the dependent callback only once ..

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Currently no - the dependent() method will permanently add a listener to that field.

    What is it you need to do?

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    @allan @colin

    this an issue if you only want the dependency when a new record is being created not when you edit an existing one.

    I have implemented the following code. Whenever I create a new record first, "dependent" works fine. The only problem is: If I edit the new or any other existing record later on the "dependency" is still there but that is undesired.

    I only get the desired result for editing if I didn't create a new record before. I really need to be able to delete the "dependent" event listener programmatically. Is there any way to get around this?

    contractFixedEditor
        .on('initEdit', function () {
            this.message( function () { return  updateElementMessage });
        })
        .on('initCreate', function () {
            this.dependent('fixed.first_payment_date', function (val, data, callback) {
                    var self = contractFixedEditor;
                    self.set( 'fixed.following_payment_day', val.substr(0,2) );
            })   
        })     
    
  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    I resolved it myself based on @allan's comments in a different thread and a look at Editor's really abstract source code ...

    contractFixedEditor
        .on('initEdit', function () {
            $( this.field('fixed.first_payment_date').node() ).off( 'change keyup' );
            this.message( function () { return  updateElementMessage }); 
        })
        .on('initCreate', function () {
            this.dependent('fixed.first_payment_date', function (val, data, callback) {
                var self = contractFixedEditor;
                self.set( 'fixed.following_payment_day', val.substr(0,2) );
            })           
        })
    

    This line kills the dependent event listener:

    ..................
    $( this.field('fixed.first_payment_date').node() ).off( 'change keyup' );
    

    @vincmeister @kaustubh.agrawal2000 thanks for asking those questions. Hope this helps you guys, too.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi,

    Thanks for posting back with your findings. I must admit I hadn't considered the case when you might want dependent on a create form but not on an edit form.

    I've created a feature request for this in our bug tracker. Until that is developed, using the off method as you describe @rf1234 is the way to do it (since that is basically all the un-dependent would do itself).

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    @allan , thanks!
    Just to give you the business background for this:
    Creating a new record (here a loan contract) you want to preset some of the fields with what you've entered in other fields. Here: You want to preset the day of all following payments based on the first payment date. You enter the first payment date first and in a second step you can adjust the day for all future payments after the first payment.
    Of course when opening the Edit form you don't want your saved future payments day
    to get overwritten with the day of the first payment - and accidentally saved when making other changes.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Quick update on this - Editor 1.9 is going to have an undependent() method which can be used to remove dependencies added using dependent().

    Allan

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    :+1:

This discussion has been closed.