Skip to content

Commit d3b0bf4

Browse files
author
andrewblond
committed
Implemented bh.require
1 parent c64baf3 commit d3b0bf4

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,24 @@ bh.apply({ block: 'button', mods: { disabled: true } });
221221
<div class="button button--disabled"></div>
222222
```
223223
224+
## External modules
225+
226+
To external modules are available in templates need to write them into namespace `bh.lib`.
227+
228+
```javascript
229+
bh.lib.i18n = BEM.I18N;
230+
```
231+
232+
To get the external module in templates need to use `bh.require ('depend-name')` method.
233+
234+
```javascript
235+
var i18n = bh.require('i18n');
236+
237+
bh.match('button', function (ctx) {
238+
ctx.content(i18n('button', 'action'));
239+
});
240+
```
241+
224242
## Additional examples
225243
226244
For example, if you want to set `state` modifier with `closed` value for all blocks do the following:

README.ru.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,24 @@ bh.apply({ block: 'button', mods: { disabled: true } });
220220
<div class="button button--disabled"></div>
221221
```
222222
223+
## Внешние модули
224+
225+
Чтобы внешние модули были доступны в коде шаблонов нужно записать их в `bh.lib`.
226+
227+
```javascript
228+
bh.lib.i18n = BEM.I18N;
229+
```
230+
231+
Для получения внешнего модуля в коде шаблонов следует использовать `bh.require('depend-name')` метод.
232+
233+
```javascript
234+
var i18n = bh.require('i18n');
235+
236+
bh.match('button', function (ctx) {
237+
ctx.content(i18n('button', 'action'));
238+
});
239+
```
240+
223241
## Дополнительные примеры
224242
225243
Например, мы хотим установить модификатор `state` со значением `closed` для всех блоков `popup`:

lib/bh.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,18 @@ BH.prototype = {
670670
return this;
671671
},
672672

673+
/**
674+
* Возвращает модуль по имени из неймспейса `bh.lib`.
675+
* ```javascript
676+
* var i18n = bh.require('i18n');
677+
* ```
678+
* @param {String} name
679+
* @returns {*}
680+
*/
681+
require: function(name) {
682+
return this.lib[name];
683+
},
684+
673685
/**
674686
* Объявляет глобальный шаблон, применяемый перед остальными.
675687
* ```javascript

test/test.require.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var BH = require('../lib/bh');
2+
require('chai').should();
3+
4+
describe('bh.require()', function() {
5+
var bh;
6+
beforeEach(function() {
7+
bh = new BH();
8+
});
9+
10+
it('should have lib namespace', function() {
11+
bh.lib.should.eql({});
12+
});
13+
14+
it('should require depend by name', function() {
15+
bh.lib.depend = 'value';
16+
17+
bh.require('depend').should.equal('value');
18+
});
19+
20+
it('should export undefined if depend is not found', function() {
21+
var depend = bh.require('depend');
22+
23+
(typeof depend).should.equal('undefined');
24+
});
25+
});

0 commit comments

Comments
 (0)