Dear everyone,

I have been building a custom emailing module similar to the "private messaging" module. I had to build a new module because I couldn't customize "private messaging" to my needs. The problem I have is with Drupal.Behaviors/Jquery script. For the sake of simplicity I have two behaviors in the script: One for deleting messages from inbox, and the other for returning deleted messages from trash to inbox back again. I have arranged two links in the module, one for "delete" and the other to "return" The links are generated in the module like this:

// Generating the link for deleting the message
$query = db_select....
$data = $query -> execute();	
$i =0;       
$delete_mid = array();
foreach ($data as $r) {
.
$delete_mid[] = $r->mid;
.
$rows[] = array(
              array('data' => '<p id = '."$delete_mid[$i]".' >'),
			);
		$i = $i + 1;
}
drupal_add_js(array('My_module' => array('delete_message_mid' => $delete_mid)), array('type' => 'setting'));

And somewhere else:

// Generating the link for returning the message
$query = db_select....
$data = $query -> execute();	
$i =0;       
$return_mid = array();
foreach ($data as $r) {
.
$return_mid[] = $r->mid;
.
$rows[] = array(
              array('data' => '<p id = '."$return_mid[$i]".' >'),
			);
		$i = $i + 1;
}
drupal_add_js(array('My_module' => array('return_message_mid' => $return_mid)), array('type' => 'setting'));

Then the Scripts:

(function( $ ) {  
//  Delete message from inbox   
Drupal.behaviors.My_module_message_delete= {
    attach: function (context, settings) {
	var js_delete_mid = Drupal.settings.My_module.delete_message_mid ;
    var text = "";
	for (var i = 0; i <js_delete_mid.length; i++) {
	$( "#" + js_delete_mid[i] ).click(function () {
    var currentId = $(this).attr('id');
		 $.ajax({
                 url:"http://localhost/drupal/messages/delete",
	         data: { js_delete_mid: currentId },
                 type: "GET",
                 dataType : "text",
                 })
            location.reload();
    	    });
}}};   
  
//   Delete message from trash        
Drupal.behaviors.My_module_message_return= {
    attach: function (context, settings) {
	var js_return_mid = Drupal.settings.My_module.return_message_mid ;
	var text = "";
	for (var n = 0; n <js_return_mid.length; n++) {
	$( "#" + js_return_mid[n] ).click(function () {
    var currentId = $(this).attr('id');
		 $.ajax({
                 url:"http://localhost/drupal/messages/trash_delete_and_return",
	         data: { js_return_mid: currentId },
                 type: "GET",
                 dataType : "text",
                 })
            location.reload();
    	    });
}}};   
})(jQuery);
 

The problem - weird as it is to me! - is that both scripts works normally each by itself ALONE. If I put both of scripts together I get this errors in Chrome inspectors:

Uncaught TypeError: Cannot read property 'length' of undefined
    at Object.attach (My_module.js?oiu0xv:121)
    at Object.<anonymous> (drupal.js?oiu0xv:76)
    at Function.each (jquery-1.10.2.js:665)
    at Object.Drupal.attachBehaviors (drupal.js?oiu0xv:74)
    at HTMLDocument.<anonymous> (drupal.js?oiu0xv:481)
    at fire (jquery-1.10.2.js:3048)
    at Object.fireWith [as resolveWith] (jquery-1.10.2.js:3160)
    at Function.ready (jquery-1.10.2.js:433)
    at HTMLDocument.completed (jquery-1.10.2.js:104)

Can somebody please tell me what I'm doing wrong ? thanks you all in advance.

Comments

Jaypan’s picture

The hint is in this part of the error:
Cannot read property 'length' of undefined

It means either js_delete_mid or js_return_mid has not properly been sent from PHP to your JS. You need to look at what is happening on the PHP side of things, and why this is not being sent.

Amjad_dokhan’s picture

You're absolutely right Jaypan. I adjusted my script to this:

Drupal.behaviors.My_module_message_delete= {
    attach: function (context, settings) {
	var js_delete_mid = Drupal.settings.My_module.delete_message_mid ;
        
       if (js_delete_mid  && js_delete_mid .length) {      // <-- Added this here !

       var text = "";
	for (var i = 0; i <js_delete_mid.length; i++) {
	$( "#" + js_delete_mid[i] ).click(function () {
            var currentId = $(this).attr('id');
		 $.ajax({
                 url:"http://localhost/drupal/messages/delete",
	         data: { js_delete_mid: currentId },
                 type: "GET",
                 dataType : "text",
                 })
            location.reload();
    	    });
   }  // And enclosed it with this !
}}};   

Then is my module I changed this:

 drupal_add_js(array('My_module' => array('return_to_inbox' => $delete_message_mid )), array('type' => 'setting'));

To this:

if (!empty($return_to_inbox)) {
 drupal_add_js(array('My_module' => array('return_to_inbox' => $delete_message_mid )), array('type' => 'setting'));
}

Now it works like a charm. Thank you very much again for your prompt reply. Have a good day !