0

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.

Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
Nick
  • 1,082
  • 1
  • 15
  • 27

2 Answers2

2

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.

Looking for a solution?

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.

Community
  • 1
  • 1
Hashem Qolami
  • 97,268
  • 26
  • 150
  • 164
  • 1
    Thank you, learned a lot from this. I have decided to use set_userdata with a database-stored session. Not entirely sure if I'm happy with that because it's going to have to store a lot of data per user, but I probably wont have too many anyway. – Nick Jan 16 '14 at 13:12
0

Every ajax request is a new page load, so, when you call it first, you load the model and set you variable, but when you call the second request you load a new instance of the model, with new values for your variables... I think the best way to manage this is using SESSIONS or COOKIES...

giordanolima
  • 1,190
  • 1
  • 12
  • 21
  • So basically the ajax call recreates the entire CI project just for the ajax call? If anyone call tell me what would be the best solution for this that would be great, but storing anything client-side is not a solution for me. Storing everything in a database doesn't seem like the best solution either. – Nick Jan 16 '14 at 13:00
  • 1
    basically yes... Every ajax request you do is like you press F5 buttom... or click in a link into your site... – giordanolima Jan 16 '14 at 13:30