Skip to content

Commit

Permalink
Implemented bh.require
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewblond committed Jun 8, 2015
1 parent b70bfea commit 25298cb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ bh.apply({ content: '<script>' });
<div>&lt;script&gt;</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 @@ -196,6 +196,24 @@ bh.apply({ content: '<script>' });
<div>&lt;script&gt;</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 @@ -662,6 +662,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');
});
});

0 comments on commit 25298cb

Please sign in to comment.