diff --git a/README.md b/README.md index 3d5ed02..8fe26c3 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,24 @@ bh.apply({ block: 'button', mods: { disabled: true } });
``` +## External modules + +To external modules are available in templates need to write them into namespace `bh.lib`. + +```javascript +bh.lib.i18n = BEM.I18N; +``` + +To get the external module in templates need to use `bh.require ('depend-name')` method. + +```javascript +var i18n = bh.require('i18n'); + +bh.match('button', function (ctx) { + ctx.content(i18n('button', 'action')); +}); +``` + ## Additional examples For example, if you want to set `state` modifier with `closed` value for all blocks do the following: diff --git a/README.ru.md b/README.ru.md index 6c9fafc..bad28ed 100644 --- a/README.ru.md +++ b/README.ru.md @@ -220,6 +220,24 @@ bh.apply({ block: 'button', mods: { disabled: true } }); ``` +## Внешние модули + +Чтобы внешние модули были доступны в коде шаблонов нужно записать их в `bh.lib`. + +```javascript +bh.lib.i18n = BEM.I18N; +``` + +Для получения внешнего модуля в коде шаблонов следует использовать `bh.require('depend-name')` метод. + +```javascript +var i18n = bh.require('i18n'); + +bh.match('button', function (ctx) { + ctx.content(i18n('button', 'action')); +}); +``` + ## Дополнительные примеры Например, мы хотим установить модификатор `state` со значением `closed` для всех блоков `popup`: diff --git a/lib/bh.js b/lib/bh.js index 4f40338..cb44669 100644 --- a/lib/bh.js +++ b/lib/bh.js @@ -670,6 +670,18 @@ BH.prototype = { return this; }, + /** + * Возвращает модуль по имени из неймспейса `bh.lib`. + * ```javascript + * var i18n = bh.require('i18n'); + * ``` + * @param {String} name + * @returns {*} + */ + require: function(name) { + return this.lib[name]; + }, + /** * Объявляет глобальный шаблон, применяемый перед остальными. * ```javascript diff --git a/test/test.require.js b/test/test.require.js new file mode 100644 index 0000000..de4c23b --- /dev/null +++ b/test/test.require.js @@ -0,0 +1,25 @@ +var BH = require('../lib/bh'); +require('chai').should(); + +describe('bh.require()', function() { + var bh; + beforeEach(function() { + bh = new BH(); + }); + + it('should have lib namespace', function() { + bh.lib.should.eql({}); + }); + + it('should require depend by name', function() { + bh.lib.depend = 'value'; + + bh.require('depend').should.equal('value'); + }); + + it('should export undefined if depend is not found', function() { + var depend = bh.require('depend'); + + (typeof depend).should.equal('undefined'); + }); +});