A
A
Anton Dyshkant2015-04-03 05:58:22
JavaScript
Anton Dyshkant, 2015-04-03 05:58:22

How to set up and use RequireJS in CodeIgniter?

Hello!
I decided to streamline the JS code in the project on CodeIgniter, I chose RequireJS for implementation, because I considered it easy to set up and easy to use.
Faced with a misunderstanding of something.
The project has several pages, each of which has a table into which data is loaded via ajax. But on different pages, data is loaded when accessing different controllers and different methods.
Therefore, it was decided to move the table query into a separate module, passing it the controller and the method to be called as arguments. app/load_data module
:

define(
    ['jquery'],
    function($) {
        return {
            get_table : function(controller, method, callback_func) {
                $.ajax({
                    type: 'POST',
                    url: '/'+controller+'/'+method+'/',
                    success: function(answer) {
                        callback_func(answer);
                    }
                });
            },
        }
    }
);

In the end, I expected this module to lie peacefully until another module explicitly calls get_table() and passes it the controller, method, and callback arguments.
Here is the app/controller/table module calling the app/load_data module :
define(
    ['jquery', 'app/load_data'],
    function($, data) {
        return {
            get: data.get_table('controller_name', 'method_name', function(table) {
              $('.my_div').html(table);
            })
        }
    }
);

In this case, the app/controller/table module is called from the entry point:
require(
    ['app/controller/table'],
    function(table){
//        table.get;
    }
);

Yes, the table.get line is indeed commented out, but all the necessary work is done, i.e. an ajax request goes in, the data is returned, the callback inserts it into div.my_div and everything is fine :)
Obviously, I misunderstand the logic of RequireJS.
I would like the described actions to occur after I call "table.get" or something else explicitly, so that I can call it from the handler, for example.
In addition, I thought in the "app/controller/table" module to describe other actions besides getting the table (editing, deleting rows), and I also need everything to be done not automatically, but on command :)
What am I doing wrong? How to prevent auto-execution of the described and make it execute on call?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Dyshkant, 2015-04-03
@vyshkant

The issue was resolved as follows: rather simple and obvious changes were made to the app/controller/table
module :

define(
    ['jquery', 'app/load_data'],
    function($, data) {
        return {
            get: function(){
                data.get_table('controller_name', 'method_name', function(table) {
                  $('.my_div').html(table);
                });
            }
        }
    }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question