TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-11-2007, 11:51 PM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Classes and Prototype [HELP]

Hello,

I've made a 'comments' function on my website's user profile page, and when a comment is posted, it posts/appears instantly, with the magic of AJAX. I was looking through the Prototype API documents to see if they've added anything new to go along with the new 1.6 release. They've changed up there 'Class' function to make it easier to use. Now I've never actually never used classes, and not just in Javascript... in PHP neither. (I need to convince myself it's not too scary to start to learn it, lol) I decided to have a crack at the Prototype's class functions and I thought if I could get the concepts of it, I'll re-write my comment posting function to work with a class, that way I can add to it, like for when I want to delete comments in the same way.

I did a little test, something simple:

Code:
<a href="#" onclick="effect.show(); return false">Show</a>&nbsp;<a href="#" onclick="effect.hide(); return false">Hide</a>
<div id="holder">This is some content</div>

<script language="javascript">
  var Comment = Class.create({
    initialize: function(element) {
	  this.element = element;
	},
	
	show: function() {
	  new Effect.Appear(this.element);
	},
	
	hide: function() {
	  new Effect.Fade(this.element);
	}
  });
  
  var effect = new Comment('holder');
</script>
Success! I managed it with little effort, so I thought I'd have a crack at re-writing my function, but I've come to a complication, and I can't see anything in the Prototype documents that could help, so hopefully someone here could help; I've seen some people here know a thing or two about Prototype.

Here's my function:

javascript Code:
var Comments = Class.create({
    initialize: function(form) {
      username = '{/literal}{$smarty.get.uid}{literal}';
      this.username = username;
      this.form = form
    },
    // Post a comment
    post: function() {
      if(Spry.Widget.Form.validate(this.form) == true) {
        new Ajax.Updater('user_comments', 'configs/ajax_functions.php?profile_comment=post', {
          insertion: Insertion.Top,
          parameters: {
            user: this.username,
            comment: $F('comment')
          },
          onLoading: function() {
            $('comment_button').disable().value = 'Adding Comment';
            $('comment').clear().disable()
          },
          onSuccess: function() {
            $('comment_button').value = 'Comment Posted';
          }
        });
      }
    }
  });
 
  var comment = new Comments('post-comment');
Then on my form, I use onSubmit like so:

html Code:
<form id="post-comment" onsubmit="comment.post(); return false">
Now when I submit the form, in Firebug I get an error saying:

Quote:
comment.post is not a function
I've tried moving stuff around, but to no avail, it always comes back with that error.

If anybody has any idea on my situation, I look forward to a response from ya :)

Thanks a lot!
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 11-12-2007, 03:25 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Your code looks fine. Have you made sure you've got the latest version of Prototype as you're using the latest syntax for creating class', which wont work with older versions.

I created a skeleton of your Comemnts class and it worked fine for me, so you could use this as a test.

Code:
<html>
<head>
<script language="JavaScript" src="prototype.js"></script>
<script language="JavaScript">

var Comments = Class.create(
{
	initialize: function() 
	{
		alert('init');
  	},
  
  	// Post a comment
  	post: function() 
  	{
  		alert('post');
  	}
});

var comment = new Comments(); 
comment.post();
        
</script>
</head>
</html>
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-12-2007, 05:31 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I suppose the question is, what version of Prototype are you using, CMellor?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-12-2007, 08:17 PM   #4 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

The newest one, 1.6.

I tried your code Karl but got some errors about the initialize function, which is weird because it looks good to me.

I'll keep trying I guess...
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 11-12-2007, 09:14 PM   #5 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Weird, it just wasn't working with the inline event handler, so I used an Event.observer

Code:
Event.observe('post_comment', 'submit', function() {
	comment.post();
});
and it worked, but that lead to another error *sigh*

I have it so if the textarea is left blank upon submit, it will show an error and do nothing more, I'm using Spry, which has it's own method's of doing things. It's simply:

Code:
if(Spry.Widget.Form.validate(this.form) == true) {
  alert('Test');
}
If the textarea wasn't blank, then the alert would appear. What I'm getting is, when I submit the blank form, I get the alert and then the error message... it didn't do this in the normal function, but it's doing it in the class, which is weird.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 11-13-2007, 07:42 PM   #6 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Hmm, everything looks fine. What error are you getting and what line/section of code is the error pointing to.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:50 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design