melenaos / jquery-punchcard

Github punchcard inspired jQuery plugin
MIT License
48 stars 4 forks source link

Support Promise and Function in data key #6

Closed pisinr closed 7 years ago

pisinr commented 7 years ago

This allows data key to be more extensible.

  1. It can be regular data array, like before.
  2. It can be a Promise (anything with a then method) that resolves to data array.
  3. It can be a function that returns a data array.
  4. It can be a function that returns a promise that resolves to data array.

In case of (3.) and (4.), this allows refreshing data or recalculating data.

So this will make ajax call on first render, but not on refresh:

$('#punchcardExample').punchcard({
    data: $.getJSON('/data.json')
})

While this will make new GET request on refresh

$('#punchcardExample').punchcard({
    data: function(){ return $.getJSON('/data.json'); }
})

The function above is actually called with function(this.settings, this). So you can share a single function to handle multiple punchcard.

Other related changes include:

  1. Adding class punchcard-loading while the Promise is being resolved.
  2. Adding class punchcard-error is the Promise fails.
melenaos commented 7 years ago

Perfect, thanks

pisinr commented 7 years ago

@melenaos Do you know how do I make sure that this count toward hacktoberfest?

melenaos commented 7 years ago

I am not sure, as I read at the instructions it just need a pull request to be accepted. I have added hacktoberfest at the pull request, it might be needed. If you find out more on this please let me know.

pisinr commented 7 years ago

Thank a lot!