سؤال

أنا جديد على Mootools وأقوم بإنشاء فئة قالب ، هذا هو الكود الخاص بي -

var Template = new Class({
  Singleton : true,
  template : '',

  /* gets the component type template */
  get : function(componentType){
    var tplUrl = Core.getUrl('backend') + 'response/' + componentType + '/get_template.php',
        that = this,
        request = new Request({url: tplUrl, method : 'get',onSuccess : function(responseText){
          that.template = responseText; 
          return that;
        }}).send(); 
  }

});

ما أريد فعله هو:

var tpl = new Template();
tpl.get('component').setTemplateData({name:'yosy'});

المشكلة هي عندما أتصل بهذا الرمز:

var tpl = new Template();
console.log( tpl.get('component') );

أنا لا أحصل على كائن القالب الحالي الخاص بي ، وأنا أحصل على "غير محدد".
كيف يمكنني جعل هذه السلسلة قابلة للسلسلة؟

هل كانت مفيدة؟

المحلول

أنت تقوم بإجراء مكالمة غير متزامنة داخل get وظيفة. قد يستغرق الطلب 100 مللي ثانية أو 1s أو 10s وبحلول الوقت get الوظيفة تنتهي وإرجاع ، سيظل الطلب معلقًا. بدلاً من ذلك ، ما عليك فعله هو ، تمرير وظيفة رد الاتصال إلى get ودعا ذلك على النجاح.

get: function(componentType, successCallback) {
    var request = new Request({
        ..,
        onSuccess: successCallback
    }).send();
}

لاحظ أنك لا تعيد أي شيء من وظيفة GET. مثال واحد لاستدعاء هذا سيكون:

tpl.get('component', function(responseText) { alert(responseText); });

نصائح أخرى

وظيفة الحصول على فقدان قيمة الإرجاع. إذا كنت تريد سلسلة لسلسلة ، فيجب عليك إرجاع الكائن نفسه:

get : function(componentType){
var tplUrl = Core.getUrl('backend') + 'response/' + componentType + '/get_template.php',
    that = this,
    request = new Request({url: tplUrl, method : 'get',onSuccess : function(responseText){
      that.template = responseText; 
      return that;
    }}).send(); 

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