Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,24 @@ bh.apply({ block: 'button', mods: { disabled: true } });
<div class="button button--disabled"></div>
```

## 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:
Expand Down
18 changes: 18 additions & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ bh.apply({ block: 'button', mods: { disabled: true } });
<div class="button button--disabled"></div>
```

## Внешние модули

Чтобы внешние модули были доступны в коде шаблонов нужно записать их в `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`:
Expand Down
12 changes: 12 additions & 0 deletions lib/bh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/test.require.js
Original file line number Diff line number Diff line change
@@ -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');
});
});