javascript - Standard way to pass jQuery references to callbacks or plugins -
What is the recommended way to pass cached jQuery references, e.g. To work as a callback, Example: In the main script Standard document ready Should there be a way to use the inline function to wrap the callback function name? Any signs for standard practice will also be welcome. You can follow a modular approach by defining a namespace. if If you are adding objects on the fly, then inside objects you can add contexts as functions that return real object references. Whenever you need to reference an object, it is already available , So get rid of DOM. $ domContainer in
var $ domContainer = $ ('# container'); The function is defined before and outside of the
$ (document), .ready () ?
& lt; Script src = "/ plugins.js" & gt; & Lt; / Script & gt;
Function line action (event) {// How do I get a reference here in this external file of reusable tasks? // How can I use $ domTable and $ domFilters / I can use $ (event.target) and go back to dom // back to $ domTable, but $ domTable is not defined in external file , Although there is no reference in the local realm of cached // $ (document) .ready (); // Similarly, $ domTable can be accessed via event.delegateTarget //, however, how can I pass $ domFilters and other wars? }
& lt; Script src = "/ scripts.js" & gt; & Lt; / Script & gt;
$ (document) .ready (function () {// Cached reference var $ domFilters = $ ('# form filter' ), More than 1 reference returned $ $ domTable = $ ('# result'); // 1 reference returned $ domTable.on ('click', '. Menu', line action); // reference function plug-in. Js // How can I insert a DOM filter to avoid $ lookups? // I had $ domFilters plugins like $ (...) plugins ({F: $ domFilters}); // If the pass was optional argument, But if this is not a plugin, then do this What's new // equal way?});
// These four open work objects, handlers, bindings, namespaces = {//, where you will cache the references: {domcontainer: $ ('. Domcontainer')}, // Do events for binding binding to handlers: Function () (domcontainer.on ('click', handlers.clickClallback)} // Actual handler handler: {clickClall: function (e) {// something}}, // Initial Things init: function () {objects = this.objects; handler = this .honders; // This handler will start binding. This.bindings ();}}; $ (function () {NameSpace.init ();}) ;
Comments
Post a Comment