Jump to content

OOP Won't Return Values


unemployment

Recommended Posts

Ok so i'm going to try and be as descriptive as possible for this issue is extremely challenging for me to resolve.

 

I am creating an auto suggest search feature and I decided to do it in OOP.

 

My current set up is like this...

 

function associate_search(){
     this.autosuggest_result = autosuggest_result;
     this.fill_list          = fill_list;

     function autosuggest_results(info) { // info is the value of the search field
        ajax.get('/assets/ajax/associates_search.php?info=' + info + '&sid=' + hex_md5(sess_id()) + '&list=' + with_u_list, function(resp)
        {
            var search   = eval('(' + resp + ')');
            
	if(search.length > 0){
		fill_list(search);
                }
        });
     }
   
     function fill_list (search_data){
        // processes the data into arrays and creates the searchable elements
        var search = search_data;

        return search
     }
}

var associate_search    = new associate_search;

 

In a different file, I am trying to display the returned data in my console by doing this but it shows up undefined.

 

with_field.onkeyup = function(event)
{
        var test = associate_search.autosuggest_results(with_field.value);
        console.log(test);
}

 

Am I returning the value the wrong way?

 

 

 

Link to comment
Share on other sites

You're on the right track. You need to treat the function like an object (well it is an object) and define the function as a property of the object:

 

this.autosuggest_results = function(info) {

 

That makes it accessible to the outside world, as you're only calling an object's property (which is actually a function).

 

You asked a similar question the other day, but in that case you were trying to access a function within a function, within an object -- try to ignore the fact that a function is actually an object in JavaScript for the moment. You can't do that in the way you were thinking and would have meant a fundamental re-structure of your code (which is why I didn't go into greater details at the time). I'm not about to go into great detail about how JS objects work now either (there's plenty of information on the internet and in books) but consider this example:

 

function ParentObject() {
    this.level1 = new ChildObject;
}

function ChildObject() {
    this.level2 = function() {
        alert('Calling a function within an object, within an object');
    }
}

var instance = new ParentObject;
instance.level1.level2();

 

That would achieve what you were wanting, but the logic and structure behind it is much different to the code you had. Think about the objects you have available in JS already; "document" is a child object of "window". The DOM tree is built of objects within objects, but they're not one large inherent object, they're broken down into smaller objects. If this is all very puzzling I'd read up on some OO theory before continuing.

Link to comment
Share on other sites

Yes, this post is related to the problem I had the other day.  I have just been doing everything I can to hash out this one problem of passing through my json encoded ajax data to my onkeyup event and I'm stuck :( . Perhaps I need to read up on OOP a bit more.  I understood everything you said and I actually had already defined

 

this.autosuggest_results	= autosuggest_results;

 

but I still wasn't receiving the returned data.  MY guess was that I wasn't receive the returned data because I wasn't using an ajax call back function so I was trying to return data that didn't exist yet.  You can get further info on my issue at http://www.phpfreaks.com/forums/index.php?topic=337468.0

 

If I could just access the data I need to then I would be done with this auto suggest feature as everything else is built. 

 

Can I get the data via the DOM?  If so, how do I do it?  How can I view data already added to the DOM tree?

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.