Answer the question
In order to leave comments, you need to log in
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);
}
});
},
}
}
);
define(
['jquery', 'app/load_data'],
function($, data) {
return {
get: data.get_table('controller_name', 'method_name', function(table) {
$('.my_div').html(table);
})
}
}
);
require(
['app/controller/table'],
function(table){
// table.get;
}
);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question