Last active
December 17, 2015 23:39
-
-
Save A/5691071 to your computer and use it in GitHub Desktop.
Пришел к такой структуре js-модулей.
Создаю инстанс так new MyModule({ // Здесь передаю параметры
});
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global shortcodeParams */ | |
/* exported MyModule */ | |
var MyModule = function (params) { | |
'use strict'; | |
var defaults = { | |
}; | |
var init = function () { | |
initParams(); | |
// ...init other stuff here... | |
}; | |
var initParams = function () { | |
params = shortcodeParams(defaults, params); | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* exported shortcodeParams */ | |
/** | |
* Combine user attributes with known attributes and fill in defaults when needed. | |
* | |
* The defaults should be considered to be all of the attributes which are | |
* supported by the caller and given as a list. The returned attributes will | |
* only contain the attributes in the $pairs list. | |
* | |
* If the params list has unsupported attributes, then they will be ignored and | |
* removed from the final returned list. | |
* | |
* | |
* @param array defaults Entire list of supported attributes and their defaults. | |
* @param array params User defined attributes in shortcode tag. | |
* @return array Combined and filtered attribute list. | |
*/ | |
var shortcodeParams = function (defaults, params) { | |
'use strict'; | |
if (typeof params !== 'object') { return defaults; } | |
var out = {}; | |
for (var key in defaults) { | |
if (defaults.hasOwnProperty(key)) { | |
if (params[key]) { | |
out[key] = params[key]; | |
} else { | |
out[key] = defaults[key]; | |
} | |
} | |
} | |
return out; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment