Skip to content

Commit

Permalink
Merge pull request #1379 from creedencewright/smdenis.getmods.fix
Browse files Browse the repository at this point in the history
i-bem: поправлен getMods()
  • Loading branch information
Vladimir Varankin committed Jun 29, 2016
2 parents 04c64ef + 81f3b0d commit 6609b0a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
23 changes: 23 additions & 0 deletions common.blocks/i-bem/__dom/i-bem__dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,29 @@ DOM = BEM.decl('i-bem__dom',/** @lends BEMDOM.prototype */{
});
},

/**
* Returns values of modifiers of the block/nested element
* @param {Object} [elem] Nested element
* @param {String} [...modNames] Modifier names
* @returns {Object} Hash of modifier values
*/
getMods : function(elem) {
var hasElem = elem && typeof elem !== 'string',
modCache = this._modCache,
modNames = [].slice.call(arguments, hasElem? 1 : 0),
res = this._extractMods(modNames, hasElem? elem : undef);

if(!hasElem) { // Caching
modNames.length?
modNames.forEach(function(name) {
modCache[name] = res[name];
}) :
modCache = res;
}

return res;
},

/**
* Sets a modifier for a block/nested element
* @param {jQuery} [elem] Nested element
Expand Down
11 changes: 11 additions & 0 deletions common.blocks/i-bem/i-bem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,18 @@ describe('i-bem', function() {
delete BEM.blocks['block'];
});

describe('getMods', function() {
it('should return specified mods', function() {
block.getMods('mod1', 'mod2', 'mod3').should.be.eql({ mod1 : 'val1', mod2 : true, mod3 : false });
});
});

describe('getMod', function() {
it('should return correct value after getMods()', function() {
block.getMods();
block.getMod('mod1').should.be.equal('val1');
});

it('should return current mod\'s value', function() {
block.getMod('mod1').should.be.equal('val1');
});
Expand Down
33 changes: 11 additions & 22 deletions common.blocks/i-bem/i-bem.vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,18 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
*/
getMods : function(elem) {
var hasElem = elem && typeof elem !== 'string',
modNames = [].slice.call(arguments, hasElem? 1 : 0),
res = this._extractMods(modNames, hasElem? elem : undef);

if(!hasElem) { // caching
modNames.length?
modNames.forEach(function(name) {
this._modCache[name] = res[name];
}, this) :
this._modCache = res;
}
modCache = this._modCache,
modNames = [].slice.call(arguments, hasElem? 1 : 0);

return !modNames.length?
modCache :
modNames.reduce(function(res, mod) {
if(mod in modCache) {
res[mod] = modCache[mod];
}

return res;
return res;
}, {});
},

/**
Expand Down Expand Up @@ -525,17 +525,6 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
return '';
},

/**
* Retrieves name/value for a list of modifiers
* @private
* @param {Array} modNames Names of modifiers
* @param {Object} [elem] Element
* @returns {Object} Hash of modifier values by name
*/
_extractMods : function(modNames, elem) {
return {};
},

/**
* Returns a block's default parameters
* @protected
Expand Down

0 comments on commit 6609b0a

Please sign in to comment.