Skip to content

Commit d0d553c

Browse files
author
Denis Smirnov
committed
i-bem: поправлен getMods()
1 parent 927560b commit d0d553c

3 files changed

Lines changed: 45 additions & 22 deletions

File tree

common.blocks/i-bem/__dom/i-bem__dom.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,29 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
605605
});
606606
},
607607

608+
/**
609+
* Returns values of modifiers of the block/nested element
610+
* @param {Object} [elem] Nested element
611+
* @param {String} [...modNames] Modifier names
612+
* @returns {Object} Hash of modifier values
613+
*/
614+
getMods : function(elem) {
615+
var hasElem = elem && typeof elem !== 'string',
616+
modCache = this._modCache,
617+
modNames = [].slice.call(arguments, hasElem? 1 : 0),
618+
res = this._extractMods(modNames, hasElem? elem : undefined);
619+
620+
if(!hasElem) { // Caching
621+
modNames.length?
622+
modNames.forEach(function(name) {
623+
modCache[name] = res[name];
624+
}) :
625+
modCache = res;
626+
}
627+
628+
return res;
629+
},
630+
608631
/**
609632
* Sets a modifier for a block/nested element
610633
* @param {jQuery} [elem] Nested element

common.blocks/i-bem/i-bem.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,18 @@ describe('i-bem', function() {
114114
delete BEM.blocks['block'];
115115
});
116116

117+
describe('getMods', function() {
118+
it('should return specified mods', function() {
119+
block.getMods('mod1', 'mod2', 'mod3').should.be.deep.equal({ mod1 : 'val1', mod2 : true, mod3 : false });
120+
});
121+
});
122+
117123
describe('getMod', function() {
124+
it('should return correct value after getMods()', function() {
125+
block.getMods();
126+
block.getMod('mod1').should.be.equal('val1');
127+
});
128+
118129
it('should return current mod\'s value', function() {
119130
block.getMod('mod1').should.be.equal('val1');
120131
});

common.blocks/i-bem/i-bem.vanilla.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,18 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
331331
*/
332332
getMods : function(elem) {
333333
var hasElem = elem && typeof elem !== 'string',
334-
modNames = [].slice.call(arguments, hasElem? 1 : 0),
335-
res = this._extractMods(modNames, hasElem? elem : undef);
336-
337-
if(!hasElem) { // caching
338-
modNames.length?
339-
modNames.forEach(function(name) {
340-
this._modCache[name] = res[name];
341-
}, this) :
342-
this._modCache = res;
343-
}
334+
modCache = this._modCache,
335+
modNames = [].slice.call(arguments, hasElem? 1 : 0);
336+
337+
return !modNames.length?
338+
modCache :
339+
modNames.reduce(function(res, mod) {
340+
if(mod in modCache) {
341+
res[mod] = modCache[mod];
342+
}
344343

345-
return res;
344+
return res;
345+
}, {});
346346
},
347347

348348
/**
@@ -525,17 +525,6 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
525525
return '';
526526
},
527527

528-
/**
529-
* Retrieves name/value for a list of modifiers
530-
* @private
531-
* @param {Array} modNames Names of modifiers
532-
* @param {Object} [elem] Element
533-
* @returns {Object} Hash of modifier values by name
534-
*/
535-
_extractMods : function(modNames, elem) {
536-
return {};
537-
},
538-
539528
/**
540529
* Returns a block's default parameters
541530
* @protected

0 commit comments

Comments
 (0)