jQuery and “Organized Code”

后端 未结 8 1781
星月不相逢
星月不相逢 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:08

    Well, for one, having a good IDE that understands javascript can help tremendously, even if just to identify matching demarcations (braces, parens, etc).

    If your code starts to really get that complex, consider making your own static object to organize the mess - you don't have to work so hard to keep everything anonymous.

    var aCustomObject = {
        container: $("#inputContainer"),
        initialize: function()
        {
            for(var i=0; i < 5; i++)
            {
                $("").changed( aCustomObject.changeHandler );
            }
        },
        changeHandler: function( event )
        {
            $.ajax( {success: aCustomObject.ajaxSuccessHandler} );
        },
        ajaxSuccessHandler: function( data )
        {
            $.each( aCustomObject.container.children(), aCustomObject.updateBindings )
        },
        updateBindings: function( j, w )
        {
            $(w).unbind().changed( function(){} );
        }
    }
    aCustomObject.initialize();
    

提交回复
热议问题