SORU
30 EKİM 2008, PERŞEMBE


bir WordPress kullanmak ve "Organize Kod"

Son zamanlarda jQuery kodlarını düzenlemek için en iyi yolu anlayışı ile mücadele edip duruyorum. Başka bir soru yeterince açık olduğumu sanmıyorum daha önce (found in this question here) sordum.

Benim sorunum daha zengin bir uygulama yapmak hızlı, müşteri tarafını kontrol dışarı çıkar. Bu durumu göz önünde bulundurun...

//Let's start some jQuery
$(function() {        
    var container = $("#inputContainer");

    //Okay let's list text fields that can be updated
    for(var i=0; i < 5; i  ) {

        //okay let's add an event for when a field changes
        $("<input/>").change(function() {

            //okay something changed, let's update the server
            $.ajax({
                success:function(data) {

                    //Okay - no problem from the server... let's update
                    //the bindings on our input fields
                    $.each(container.children(), function(j,w) {

                        //YIKES!! We're deep in here now!!
                        $(w).unbind().change(function() {

                            //Then insanity starts...

                        }); // end some function

                    }); //end some loop

                } // what was this again?

            }); //ending something... not sure anymore

        }).appendTo(container); //input added to the page... logic WAY split apart

    }; //the first loop - whew! almost out!

});  //The start of the code!!

Şimdi bu durum çok da imkansız değil. Ben demiyorum bunu doğru şekilde yapmak, ama değil nadir bulmak kendinizi birkaç basamak aşağı içine bir jQuery komut ve başlangıç için acaba daha ne kadar mantık ekleyebilirsiniz ekrandan önce başlar erimeye.

Benim soru nasıl bu insanları yönetmek ya da onların kod karmaşıklığını sınırlamak düzenliyor.

I listed how I'm doing it in my other post...

CEVAP
31 EKİM 2008, Cuma


Sadece daha önce bu söz ne eklemek istiyorum:

$.each(container.children(), function(j,w) {
    $(w).unbind().change(function() { ... });
});

için optimize edilebilir:

container.children().unbind().change(function() { ... });

Zincirleme kodunuzu basitleştirmek için harika bir yoldur.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Canal de arkyoru

    Canal de ark

    18 Ocak 2007
  • Awesomesauce Network

    Awesomesauce

    4 EKİM 2012
  • Professor Messer

    Professor Me

    27 NİSAN 2007