سؤال

وأساسا كنت أحاول أن تكرار واحد من الأشياء التي xajax أعطاني مع مسج -> القدرة على تحديد ما يعدل ردي جانب الخادم دون وضع أي شيء يصل العميل. يعمل تماما كما هو، ولكن في كل مرة أريد أن استدعاء دالة مسج مختلفة، لا بد لي من إضافته إلى بلدي إذا كانت تصريحات.

وكل عنصر في مجموعة استجابة يحتوي على شيء يجب أن يتغير العميل.

وأنا يجب أن تكون قادرة على إخراج ما يقرب من جميع this.action == 'بلاه "إذا كانت تصريحات واستبدال مع بعض جافا سكريبت ذكي، ولكن يبدو أنني لست ذكيا بما فيه الكفاية. :)

و$. showMessage هو مجرد استبدال لبلدي في حالة تأهب، مع مهلة وقليلا لزجة.

وموكلي مسج وظيفة:

$.doAjax = function(func,values) {
 $.ajax({ data: values, url: '/time/ajax/' + func, type: 'POST', dataType: 'json', timeout: 5000,
  error: function(xhr, ajaxOptions,thrownError){ $.showMessage('Error processing request: ' +  func,10000);$('#debug').html(xhr.responseText); },
  success: function(data){
    if(data){
  $.each(data, function(){
    if ( this.action == 'attr' ) $(this.target).attr(this.content);
    if ( this.action == 'removeAttr' ) $(this.target).removeAttr(this.content);
    if ( this.action == 'addClass' ) $(this.target).addClass(this.content);
    if ( this.action == 'removeClass' ) $(this.target).removeClass(this.content);
    if ( this.action == 'html' ) $(this.target).html(this.content);
    if ( this.action == 'text' ) $(this.target).text(this.content);
    if ( this.action == 'val' ) $(this.target).val(this.content);
    if ( this.action == 'eval' ) {
          try { 
        eval(this.content); 
      }
      catch(err) {
            $.showMessage(err,5000);
      };
    };
      });
    }
  } 
 });
 return this;
};

قانون بلدي جانب الملقم:

header("Content-type: text/plain");   
$response = array();
$response[] = array('action'=>'html','target'=>'#booked_time_group_unbilled','content'=>get_booked_time_group_unbilled());
$response[] = array('action'=>'html','target'=>'#booked_time_my_unbilled','content'=>get_booked_time_my_unbilled());
$response[] = array('action'=>'eval','target'=>'','content'=>"$.showMessage('The selected time has been deleted',5000,true)");
echo json_encode($response);
هل كانت مفيدة؟

المحلول

وهذا يجب أن تعمل:

$(this.target)[this.action](this.content);

نصائح أخرى

ولست متأكدا بالضبط ما كنت طالبا؟ أسلوبيا، أود أن أكتب مثل هذا:

var handlers = {
    attr: function() { $(this.target).attr(this.content) },
    removeAttr: function() { $(this.target).removeAttr(this.content) },
    // etc.
};

$.each(data,function() {
   handlers[this.action].call(this);
});

إذا كنت ترغب فقط في تنفيذ كل ما يرسل الملقم مرة أخرى، والتي لا فقط أرسل دائما العودة إلى العمل حدة التقييم؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top