简体   繁体   中英

How to extend called callback from another function

I'm not sure about what title I need to use or what I need to call what I'm gonna do, here is the problem

there is a code in sortable.js

var sortableStuff;

    sortableStuff = {

        sortableList = $('#mySortable');

        init: function(){

           sortableList.sortable({

              start: function(){

                  lot of codes here... 

              },

              stop: function() {

                  another codes here..
              }
           });
        }

    }

I want to extend the start and stop function from another another file for example mysortable.js but still run the start and stop callback from the one above.

sortableStuff.sortableList.sortable({
       start: function(){
           run the code from the start before..

           run my new code
       }
})

I'm not a javascript expert, and don't know what to call what I'm trying to do here, please help me.

You've got a syntax error in your first script, mixing object literals with variable assignments. It should be

var sortableStuff = {
    sortableList: $('#mySortable'),
//              ^                 ^
    init: function() {
        this.sortableList.sortable({
//      ^^^^^ use as a property
            start: function(){
                // lot of codes here... 
            },
            stop: function() {
                // another codes here..
            }
        });
    }
};

Now you can access sortableStuff.sortableList from outside, and depending on how your sortable plugin works be able to extend/add/overwrite callbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM