wix-incubator / react-templates

Light weight templates for react
https://wix.github.io/react-templates
MIT License
2.82k stars 207 forks source link

rt-scope along with rt-repeat fails #160

Closed nippur72 closed 8 years ago

nippur72 commented 8 years ago

The following template

<div rt-scope="[1,2,3] as z" rt-repeat="item in z">    
   <div>{item}</div>      
</div>

produces

Uncaught ReferenceError: z is not defined

The problem seems to be rt-scope and rt-repeat attributes appearing on the same tag.

Anyway by looking at the source code, this seems something that has been worked on and that presumably worked in the past:

// Order matters. We need to add the item and itemIndex to context.boundParams before // the rt-scope directive is processed, lest they are not passed to the child scopes

and also:

// Order matters here. Each rt-repeat iteration wraps over the rt-scope, so // the scope variables are evaluated in context of the current iteration.

As a fix to this problem, I propose to change the way injected functions are generated. Instead of having them all at the top level, we can nest functions the same way they appear in the template, thus making scope/naming automatic.

Example of how would be rendered the template shown above:

var templateRT = function () {
    function scope1() {
        function repeat2(item, itemIndex) {
            return React.createElement('div', {}, React.createElement('div', {}, item));
        }
        var z = [1, 2, 3];
        return _.map(z, repeat2.bind(this));
    }
    return scope1.apply(this);
};

This change would be beneficial also for #156

nippur72 commented 8 years ago

ok, got it wrong, actually the current behavior is adherent to the docs:

When used with rt-repeat, the rt-scope is evaluated for every iteration, so that iteration's item and itemIndex are in scope.