jQuery and “Organized Code”

后端 未结 8 1798
星月不相逢
星月不相逢 2020-12-12 11:52

I\'ve been struggling lately with understanding the best way to organize jQuery code. I asked another question earlier and I don\'t think I was specific enough (found in thi

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 12:25

    So far, I do it like this:

    // initial description of this code block
    $(function() {        
        var container = $("#inputContainer");
    
        for(var i=0; i < 5; i++) {
            $("").changed(inputChanged).appendTo(container);
        }; 
    
        function inputChanged() {
            $.ajax({
                success: inputChanged_onSuccess
            });
         } 
    
         function inputChanged_onSuccess(data) {
            $.each(container.children(), function(j,w) {
              $(w).unbind().changed(function() {
                 //replace the insanity with another refactored function
              });
            });
          }
    });
    

    In JavaScript, functions are first-class objects and can thus be used as variables.

提交回复
热议问题