nazar-pc / PickMeUp

Really simple, powerful, customizable and lightweight standalone datepicker
BSD Zero Clause License
616 stars 191 forks source link

Async userdata loading #222

Closed little-brother closed 4 years ago

little-brother commented 4 years ago

I want to mark some days depending on the result of the fetch being executed for each redraw. I inspected the code and found that it could be done through a change of fill-method e.g.

function fill (target) {
    var root_element = target.__pickmeup.element,
    ...
    if (!options.userdata && options.update_userdata) {
        return options.update_userdata(function (err, res) {
            options.userdata = err || res; 
            fill(target);
        });
    }
    ...
    options.userdata = null;
    dom_dispatch_event(target, 'fill');
}

And usage

pickmeup('.single', {
    update_userdata: (cb) => setTimeout(() => cb(err, [1,2,3]), 1000),
    render: function (date) {
        return (this.userdata || []).indexOf(+date.getDay()) != -1 ? 
            {class_name: 'red'} : 
            {};
    }
});

But I don't like it :( Maybe there is a more elegant solution?

nazar-pc commented 4 years ago

Try adding listener to fill event and just call .update() method whenever you want to re-render datepicker.

little-brother commented 4 years ago

Thanks, I'll try it :)