krescruz / angular-materialize

Angularjs directives for Materialize CSS Framework https://github.com/Dogfalo/materialize
MIT License
396 stars 129 forks source link

pickadate: Updating disabled dates on factory response #154

Closed eumend closed 8 years ago

eumend commented 8 years ago

I need to get reserved dates from a factory (which itself calls a server), and set them up as disabled for the materialize datepicker.

I make the API call and successfully retrieve the dates, which I save to 'disabledDates' (the model datepicker's 'disable' is bound to), but it has no effect on the datepicker. I solved that adding a watcher to the directive (which now disables the dates I retrieve), but is this the intended behaviour? Doesn't the picker support updating the 'disable' model dynamically?

HTML ('booking' equals 'vm' on the controller)

<input input-date 
    type='text' 
    ng-model='booking.unformatStartDate' 
    //other configuration
    disable='booking.disabledDates'
    >

Inside the controller ('Place' is the factory):

var vm = this;
Place.get($stateParams.place_id)
    .then(function(res){
        vm.place = res.data;
        var disabledDates = [];
        vm.place.currentReservations.forEach(function(reservation){
            disabledDates.push({
                from: new Date(reservation.start_date),
                to: new Date(reservation.end_date),
            });
        });
        vm.disabledDates = disabledDates;
    });
]);

The watcher I added as a workaround to the disable dates, right after the 'min' and 'max' watchers:

scope.$watch('disable', function(newDisabled) {
                                if( picker ) {
                                    var disabledDates = angular.isDefined(newDisabled) && angular.isArray(newDisabled) ? newDisabled : false;
                                    picker.set({disable: disabledDates});
                                }
                            });
webbiesdk commented 8 years ago

The picker doesn't currently update the disabled dynamically, because this hasn't been implemented yet.

But now you have implemented it.

And your workaround looks good (and i wouldn't call it a workaround, i would call it a fix).

So if you send a pull-request with the workaround you have described, I'll merge it.

I actually don't know the API for pickdate, are there other options that can be changed using the set method?