Explorar el Código

UserMsgs - reply test - added ajax fetch group options, fixed fetch error msg

Piotr Labudda hace 10 años
padre
commit
223f08451b
Se han modificado 1 ficheros con 34 adiciones y 13 borrados
  1. 34 13
      SE/se-lib/Route/UserMsgs.php

+ 34 - 13
SE/se-lib/Route/UserMsgs.php

@@ -740,18 +740,36 @@ function frmTestSubmit(frm) {
 <script>
 	jQuery("#widget-msg-tree").MsgTree({
 		usrLogin: '<?php echo User::getLogin(); ?>',
-		fetchOptionsForGroup: function(input, callback) {
-			var options = [];
-			for (var i = 1; i <= 100; i++) {
-				options.push({value: 'group' + i, label: 'Group [' + i + ']'});
-			}
-			callback(null, {
-				options: options,
-				// CAREFUL! Only set this to true when there are no more options,
-				// or more specific queries will not be sent to the server.
-				//complete: true
-			});
-		},
+		fetchOptionsForGroup: (function() {
+			var _groupXhr = null;
+			return function(input, callback) {
+				if (_groupXhr && _groupXhr.state() === 'pending') {
+					_groupXhr.abort();
+					_groupXhr = null;
+				}
+				_groupXhr = $.ajax({
+					url: 'index.php?_route=UserMsgs&_task=typeSpecialGroupId&q=' + input,
+					dataType: 'json'
+				});
+				_groupXhr.done(function(data, textStatus, jqXHR) {
+					if (data && data.length > 0) {
+						var options = [];
+						data.forEach(function(item) {
+							options.push({value: item.id, label: item.name});
+						});
+						callback(null, {options: options});
+					} else {
+						callback(null, {options: []});//"Error no data!");
+					}
+				});
+				_groupXhr.fail(function() {
+					callback(null, {options: []});//"Error no data!");
+				});
+				_groupXhr.always(function() {
+					_groupXhr = null;
+				});
+			};
+		})(),
 		fetchOptionsForUser: (function() {
 			var _userXhr = null;
 			return function(input, callback) {
@@ -771,9 +789,12 @@ function frmTestSubmit(frm) {
 						});
 						callback(null, {options: options});
 					} else {
-						callback("Error no data!");
+						callback(null, {options: []});//"Error no data!");
 					}
 				});
+				_userXhr.fail(function() {
+					callback(null, {options: []});//"Error no data!");
+				});
 				_userXhr.always(function() {
 					_userXhr = null;
 				});