0

When a user clicks on the Edit button, it should perform an Ajax request depending on the data attribute.

For example:

<button type="button" class="btn btn-danger btn-sm"
    data-request-type="ajax:get"
    data-request-url="/feature/edit"
>
    Edit
</button>

In Ajax success callback, how to define to past success result into custom callback or into selector?

$.ajax({
    type: requestMethod, //from data attribute
    url: requestUrl, //from data attribute
    success: function (data) {

    },
});

I am thinking doing:

data-request-success-type="callback:functionName"
data-request-success-type="selector:className"

Or is there better approach?

dakab
  • 5,379
  • 9
  • 43
  • 67
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
  • But how would a string with a functionName reference be able to access the function? Can it be done, yes, but depends on the scope for the method. – epascarello Sep 21 '16 at 12:26
  • Wouldn't putting `functionName()` in success callback work? If not, what is alternative approach. – I'll-Be-Back Sep 21 '16 at 12:27
  • Have you tries creating a separate method that accepts a parameter and then bind the onclick event to call that method with said parameter? on the succes callback just execute the parameter (in your case the function) – GxG Sep 21 '16 at 12:27
  • not when it is a string. – epascarello Sep 21 '16 at 12:27
  • `data-success-callback="functionName"` `data-success-selector="selector"` - `var callback = $that.data("success-callback"), selector = $that.data("success-selector"); if (callback) someScope[callback](data); else if (selector) $(selector).html(data);` – mplungjan Sep 21 '16 at 12:28
  • http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – epascarello Sep 21 '16 at 12:29

0 Answers0