I've been creating an application that requires buttons to make an ajax call to a controller that uses autoloaded models. I was under the impression that variables in an autoloaded model would retain their values after an Ajax call, but for some reason the entire model (including their variables) have lost their new values.
Am I misunderstanding the way the autoload function works, or does it have something to do with me using Ajax?
Code for reference below.
Autoload:
$autoload['model'] = array('choice_model');
Ajax calls in JQuery:
$( document ).ready(function() {
var encounter = 1;
$.ajax({
type: "POST",
url: baseURL+"Encounter/startEncounter",
dataType: "json",
data: "encounter_id=" + encounter,
success: function(data) {
$("#message-box").html(data);
SetChoices();
}
});
});
function SetChoices() {
$.ajax({
type: "POST",
url: baseURL+"Choice/getChoices",
dataType: "json",
data: "",
success: function(data){
alert (data);
}
});
}
The first ajax call sets the following variable in Choice_model to "TestTrue":
public $test = 'TestFalse';
The second ajax call returns the previous variable, but it's value is now "TestFalse" once again.
Thank you for your time, I hope someone can help me out.
Am I misunderstanding the way the autoload function works?
Well... yes.
Autoloading means the object is loaded (instantiated) automatically when the application starts. So we won't need to load them couple of times if needed.
Note: Only necessary/essential models should be loaded automatically.
Or does it have something to do with me using Ajax
In this case, a XHR request acts as a normal HTTP request. When you send a request to a Controller/method
the whole application runs and stops after serving the result.
So the model would lost all values stored in its properties.
I'm not sure about this, but you could store the variables in Session (by using $this->session->set_userdata()
in CI) and retrieve the stored values later.