bem / bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
https://bem.github.io/bem-xjst
Other
115 stars 47 forks source link

Introduce `addToArray` and `addMixTo` helpers for build BEMJSON #242

Open tadatuta opened 8 years ago

tadatuta commented 8 years ago

For avoid copy-paste of [{ block: 'b1' }].concat(this.ctx.mix) we can introduce two new helpers: for construct or convert to array with adding argument after and for inplace modification of mix field.

function addTo(arr, item) {
    if(typeof arr === 'undefined') arr = [];
    else if(!Array.isArray(arr)) arr = [arr];
    var i = 1;
    while(item = arguments[i++]) arr.push(item);
    return arr
}

function addMixTo(obj, item) {
    var args = Array.prototype.slice.call(arguments, 1);
    args.unshift(obj.mix);
    obj.mix = addTo(null, args);
    return obj;
}

Also need to think about similar helpers for other fields (like js, attrs and so on).

See https://github.com/bem/bem-core/issues/820 for details

miripiruni commented 8 years ago

I think we should close this issue in favor of addMix, addJs, addAttrs, etc. See https://github.com/bem/bem-xjst/pull/311