8 Replies - 635 Views - Last Post: 23 November 2011 - 12:45 AM

#1 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Functions are interpreted as not "defined"

Posted 22 November 2011 - 10:30 PM

I need to know why my functions aren't being defined. If someone could figure this out, I would consider you a god. Either that, or I'm missing something extremely obvious....

Here's my html:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">
</script>
<script type="text/javascript" src=<?=JPATH_BASE . DS . 'includes' . DS . 'js' . DS . 'functions.js';?>>
</script>

<div id="pageOne">
	<form action=<?=$saveTarget;?> method="post" enctype="multipart/form-data">
		<select name="category" id="categories" class="ads_categories" onclick="functions.sendAjaxDataViaPost('form.php', this.value);">
			<option value="0">--Please choose a category--</option>
			<?php populateCategories(0, $this->formData); ?>
		</select> 
		<select name="category" id="subcategories" class="ads_subcategories">
			<option value="0">--Please choose a subcategory--</option>
		</select> 
	</form>
</div>


As we can see, there's not much. The main issue is that when "categories" is clicked, I need that function to be called, which is located within the second .js file. While jQuery is being used in an external .js file, that shouldn't be an issue, as (at least, from what I've read on Stack Overflow), it's possible to include an external .js file with jQuery so LONG as you include the jQuery API before it.

I know for certain that is the correct path being referenced as well (for the external .js file).

Here's my javascript:

var functions = {
	
	functions.sendAjaxDataViaPost = function(pathToFile, ajaxData) {  

	    $.post(pathToFile, {'id' : ajaxData}, function(ajaxData) {
	    	
	    	alert("Data loaded: " + ajaxData);

	    	$('#pageOne').append('<pre>' + ajaxData '</pre>');

	    });
	}

	functions.initiateSubCategories = function(parent) {

	    sendAjaxDataViaPost('form.php', parent);

	    $("#pageOne").apppend('<pre>' + <?=$_POST;?> + '</pre>'));
	}

	functions.printSubCategories = function(categories) {
	    
	    for (var i = 0; i < categories.length; i++) {
	        
	        var opt = document.createElement("option");

	        opt.text = categories['name'];
	        opt.value = categories['id'];

	        $("#subcategories").append(opt);
	    }    

	} 
}


What exactly am I doing wrong? I'm stumped.

Is This A Good Question/Topic? 0
  • +

Replies To: Functions are interpreted as not "defined"

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: Functions are interpreted as not "defined"

Posted 22 November 2011 - 11:12 PM

your object syntax is totally wrong. and because of that, the functions object is not loaded and hence the called function does not exist.

syntax for the object literal:
var obj = {
    propery: "property value",
    method: function() {
        // function body
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Functions are interpreted as not "defined"

Posted 22 November 2011 - 11:55 PM

Thank you! I will try that.
Was This Post Helpful? 0
  • +
  • -

#4 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Functions are interpreted as not "defined"

Posted 23 November 2011 - 12:17 AM

I think it's more than just a syntax issue, unfortunately. I tried your suggestion, and unfortunately, it didn't work properly. Is there any other reason what the problem might be? I just keep getting undefined errors...

Here's the new and improved syntax:

function CategoriesUtility() {
	
}

CategoriesUtility.prototype.sendAjaxDataViaPost = function(pathToFile, ajaxData) {  

	    $.post(pathToFile, {'id' : ajaxData}, function(ajaxData) {
	    	
	    	alert("Data loaded: " + ajaxData);

	    	$('#pageOne').append('<pre>' + ajaxData '</pre>');

	    });
	}

CategoriesUtility.prototype.printSubCategories = function(categories) {
				    
    for (var i = 0; i < categories.length; i++) {
        
        var opt = document.createElement("option");

        opt.text = categories['name'];
        opt.value = categories['id'];

        $("#subcategories").append(opt);
    }    

}

CategoriesUtility.prototype.initiateSubCategories = function(parent){

    sendAjaxDataViaPost('form.php', parent);

    $("#pageOne").apppend('<pre>' + <?=$_POST;?> + '</pre>'));
}

var utility = new CategoriesUtility();

utility.initiateSubCategories(2);


Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: Functions are interpreted as not "defined"

Posted 23 November 2011 - 12:22 AM

did you define a function sendAjaxDataViaPost() anywhere? (there is a similar named object method)

besides that, using object construction for that simple matter seems over-complicated. why not staying at object literals?
Was This Post Helpful? 0
  • +
  • -

#6 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Functions are interpreted as not "defined"

Posted 23 November 2011 - 12:28 AM

I was studying syntax and figured I may try the prototype method, but if you suggest object literals then I'll go that way.

In answer to your query, I haven't defined another method. With this syntax used, should I prefix "sendAjaxDataViaPost" with "CategoriesUtility"?

Edit: also, do I need to instantiate a new instance of the object for it to be valid? Or can I call it as if it were static?

This post has been edited by litedrive: 23 November 2011 - 12:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: Functions are interpreted as not "defined"

Posted 23 November 2011 - 12:36 AM

View Postlitedrive, on 23 November 2011 - 08:28 AM, said:

I was studying syntax and figured I may try the prototype method, but if you suggest object literals then I'll go that way.

it indroduces one more level of complexity. there is a reason why jQuery uses callback functions whereever possible.


View Postlitedrive, on 23 November 2011 - 08:28 AM, said:

In answer to your query, I haven't defined another method. With this syntax used, should I prefix "sendAjaxDataViaPost" with "CategoriesUtility"?

that wont work, since sendAjaxDataViaPost() is not a property of CategoriesUtility but a property of its prototype. (extended level of complexity )

View Postlitedrive, on 23 November 2011 - 08:28 AM, said:

Edit: also, do I need to instantiate a new instance of the object for it to be valid? Or can I call it as if it were static?

if you have constructor instances (object created from calling with new) they can access the prototyped methods as if they were their own. "static" methods need to be called as such, since they are not part of the prototype chain.
Was This Post Helpful? 0
  • +
  • -

#8 litedrive  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 102
  • Joined: 30-September 10

Re: Functions are interpreted as not "defined"

Posted 23 November 2011 - 12:41 AM

Cool, thank you for the info.

Unfortunately, I'm still having a javascript load error issue. I think it's beyond that of syntax. Would you be able to help me with this? I've exhausted my supply of thought for this one, as I don't really know what to do. Regardless of which approach I take, I still end up with "undefined" errors.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3569
  • View blog
  • Posts: 10,403
  • Joined: 08-June 10

Re: Functions are interpreted as not "defined"

Posted 23 November 2011 - 12:45 AM

just open a new thred and thoroughly explain the issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1