Skip to content

Commit

Permalink
Added bemto_scope mixin, fixes #49
Browse files Browse the repository at this point in the history
  • Loading branch information
kizu committed Dec 30, 2015
1 parent 7d397fc commit 8d9b778
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -2,6 +2,7 @@

## v1.0.0 (in development)

- Added a `bemto_scope` mixin for setting settings at a scope level.
- Added a way to redefine prefix on block/element level.
- Added a `flat_elements` setting to allow elements of elements.
- `src`-less images now would have null gif inlined (or set to the given src).
Expand Down
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -365,6 +365,35 @@ This would render with the nested element:
</div>
```

### Scope for the settings

If you'll need to have some settings just in a certain scope, you can wrap your code in `bemto_scope` mixin, passing your desired settings right into it:

```Jade
+b.foo_bar
+bemto_scope({
prefix: 'b-',
element: '-',
modifier: '--'
})
+b.nnnn
+e.mmmm--kkkk
+e.baz
```

Would render as

```HTML
<div class="foo foo_bar">
<div class="b-nnnn">
<div class="b-nnnn-mmmm b-nnnn-mmmm--kkkk">
</div>
</div>
<div class="foo__baz">
</div>
</div>
```

## Using for building complex mixins

This is somewhat obvious, but I must mention that the bemto blocks would be great for using as the bricks for building more complex blocks. The Jade mixins work in the way where you can translate any attributes through to the any inner blocks. So you can do this:
Expand Down
52 changes: 42 additions & 10 deletions lib/settings.jade
Expand Up @@ -3,6 +3,41 @@
//- MIT Licensed
-
// Cloning via http://stackoverflow.com/a/728694/885556
function clone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
var default_bemto_settings = {
'prefix': '',
'element': '__',
Expand All @@ -15,16 +50,7 @@
var bemto_settings = default_bemto_settings;
var get_bemto_settings = function() {
var settings = bemto_settings;
//- TODO: make proper cloning?
var settings = {
'prefix': bemto_settings.prefix,
'element': bemto_settings.element,
'modifier': bemto_settings.modifier,
'default_tag': bemto_settings.default_tag,
'nosrc_substitute': bemto_settings.nosrc_substitute,
'flat_elements': bemto_settings.flat_elements
};
var settings = clone(bemto_settings);
if (bemto_settings_prefix !== undefined) { settings['prefix'] = bemto_settings_prefix; }
if (bemto_settings_element !== undefined) { settings['element'] = bemto_settings_element; }
if (bemto_settings_modifier !== undefined) { settings['modifier'] = bemto_settings_modifier; }
Expand All @@ -49,3 +75,9 @@
}
}
}
mixin bemto_scope(settings)
- var old_bemto_settings = clone(bemto_settings)
- set_bemto_settings(settings)
block
- set_bemto_settings(old_bemto_settings)

0 comments on commit 8d9b778

Please sign in to comment.