First, the demand
There are several modules in the project that use the DataTable plugin in jquery. When you start development, you use your own configuration, which results in some unified configuration of the table being dispersed across modules. Now want to extract these unified configuration into the public JS, in order to facilitate the modification of the unified configuration (man do things have a degree, not black or white, Shuizhi. Program is the same, all want to write code unified, but the function of each module is different, so always unified, feature, and then to the unified part.
Second, the programme
Generally like this configuration parameter of jquery plug-ins (DataTable, flot, etc.), there will be a default configuration, in the implementation of the basic will use the $.extend method. However, this default configuration does not necessarily meet our needs, the first of the most plug-ins in the prompt text is in English, style, and so does not necessarily conform. So we're going to define our own generic configuration, combining the special configuration and the common configuration, and then the default configuration. Search the Internet, theextend method is to combine the attributes of multiple objects, if the same property, more than one object, then use the property values of the following object.
1, first create a public.js, in each module's page to introduce the JS. In this JS, define your own generic configuration, such as Table_option, when using a DataTable, combine special configurations and common configurations in the following ways:
1 var otableoption = $.extend (table_option,{2 // special configuration or properties that require overriding of a common configuration 3 }); 4 5 var otable = $ ("#table_id"). DataTable (otableoption);
The above requirements can be achieved using this method, but when Hbuilder is used, the default parameter list is $.extend (true, target object, object) when the $.extend is knocked out.
1) First the first parameter is a Boolean type, which is checked on the Internet, and this boolean indicates whether to combine in depth, such as the following:
1 varOtarget = {2A:1,3B:2,4 c:{5Ca:1,6Cb:27 }8 };9 Ten varOobject = { OneA:2, A c:{ -Ca:2, -Cd:4 the }, - d:{ -Da:1 - } + }; - + A varOresult = $.extend (true, Otarget,oobject);
The result of the final oresult should be
Oresult = { A:2, B:2, c:{ CA:2, CB:2, CD:4 }, d:{ da:1 };
That is, if the first argument is of type Boolean and True (the default is False), a deep combination is used, otherwise the C attribute in the example is directly used for C in the Oobject object, not the C property of Otarget and Oobject.
2) from the name of the parameter, the second parameter is the target, indicating that the following object properties may be directly combined to target, which will modify the target, this is certainly not suitable for our needs, if everyone can change the table_option, When there is a problem, there is no way to fix it. Search from the Internet does not modify the target method, there is a better implementation, in fact, jquery plug-ins also use this implementation.
var ooption = $.extend (true,{},table_option,{//new plus config });
The prize target is set to an empty object, so the modification modifies only the contents of the {} object and does not change to table_option.
The $.extend method of jquery uses