ghiscoding / Aurelia-Bootstrap-Plugins

Aurelia-Bootstrap-Plugins are Custom Elements to bridge with a set of 3rd party Bootstrap addons
https://ghiscoding.github.io/Aurelia-Bootstrap-Plugins
MIT License
45 stars 23 forks source link

Resolve empty initialised value when using model.bind #85

Closed jordan-ware closed 5 years ago

jordan-ware commented 5 years ago

Binding a model but no value resulted in the value being initialised with a moment object instead of the expected formatted string, resulting in nothing appearing in the text box despite the date correctly bound behind the scenes. This change fixes this issue.

ghiscoding commented 5 years ago

I'll have to do some more tests before merging this one, the last commit I made had something similar here and it got removed. I have to see if it was causing the infinite loop or if it was just an omission. I remember that I see more problem when I use the date picker inside a modal window component, I'll have to test against it

jordan-ware commented 5 years ago

Oops, hit a wrong button sorry. I was going to say I've dealt with this issue before and realized I have my own wrapper code that deals with these syncing issues and exits as early as possible.

In the below example I'm syncing a date with a moment in my wrapper, slight changes can be made to handle the formatted string instead:

    dateValueChanged(newValue?: Date, oldValue?: Date) {
        //exits early because no actual change is detected
        if (newValue && oldValue && newValue.getTime() == oldValue.getTime()) {
            return;
        } else if (!newValue && !oldValue) {
            return;
        }
        //sync value logic below - only assign new value if it's different
        if (newValue) {
            if (this.value && this.value.valueOf() == newValue.getTime()) {
                return;
            }
            this.value = moment(newValue);
        } else {
            //this could probably be avoided too
            this.value = null;
        }
    }

*edit, apologies, it's in typescript

jordan-ware commented 5 years ago

I'll adapt this to the model/value sync if you like, cause I noticed there's still some unnecessary cycles still happening in the current implementation

ghiscoding commented 5 years ago

Finally tried it, it seems to be ok with the tests I've done.