API Plugin Development - Return namespace within API, instead of API

API Plugin Development - Return namespace within API, instead of API

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited November 2015 in Plug-ins

Ill try to keep this post short and simple :-D

Im working on a plugin, part of the plugin is implementing its own API methods, which are all inside the namespace of the API. And I know using the iterator(), you can return the API, but I wanted to return the namespace thats being used.

Example

Heres an example of the API call to abort an XHR request, then another API call to kill a loop

table.myPlugin.abort();
table.myPlugin.clear();

And if you want to chain those events, currently, you would have to do this:

table.myPlugin.abort().myPlugin.clear();

My goal is to get it so you just have to do this:

table.myPlugin.abort().clear();

So basically, instead of returning the entire API, rather just return the myPlugin namespace of the API...

Heres a snippet of the code related to the question

( function( window, document, $, undefined ) {

    var _updateLoop; // <-- This is the timeout loop 
    
    /**
     * Abort Current XHR Request
     *
     * @description: Abort the current XHR Request
     * @example: table.myPlugin.abort();
     * @return  DT API Instance
     */
    $.fn.dataTable.Api.register( 'myPlugin.abort()', function (  ) {
        return this.iterator( 'table', function ( settings ) {
            console.debug('ABORTING');
            settings.jqXHR.abort();
        } );
    } );

    /**
     * Clear Update Loop
     *
     * @description: Kill the update loop - permanently
     * @example: table.myPlugin.clear();
     * @return  DT API Instance
     */
    $.fn.dataTable.Api.register( 'myPlugin.clear()', function (  ) {
        return this.iterator( 'table', function ( settings ) {
            console.debug('CLEARING');
            clearTimeout( settings.myPlugin._updateLoop );
        } );
    } );

})( window, document, jQuery );

@allan, I have a feeling you're the only one that can help me out with this, lol. Or maybe @daniel_r

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Got it.. (Unless someone says I dont :P )

    http://live.datatables.net/diyikefo/1/edit?js,output

    $.fn.dataTable.Api.register( 'myPlugin.hello()', function () {
        return this.iterator( 'table', function ( settings ) {
            console.log('Hello');
        }, false ).myPlugin;
    } );
    
    $.fn.dataTable.Api.register( 'myPlugin.world()', function () {
        return this.iterator( 'table', function ( settings ) {
            console.log('World');
        }, false ).myPlugin;
    } );
    

    Not sure why I didn't think of that sooner.. lol

  • allanallan Posts: 61,642Questions: 1Answers: 10,093 Site admin
    Answer ✓

    Yup - that works nicely. Good idea!

    Allan

This discussion has been closed.