# JavaScript inside a component used inside Each breaks data binding

**URL:** <https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385>\
**Category:** Bug Reports\
**Created:** [March 11, 2016, 9:03am UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385 "2016-03-11T09:03:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vicker\_Leung](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/vicker_leung/32/392_2.png) [@Vicker\_Leung](https://forums.fusetools.com/u/Vicker_Leung)\
**Post date:** [March 11, 2016, 9:03am UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385/1 "2016-03-11T09:03:37Z")

</div>

I found that whenever there is a JavaScript block inside the component UX, the data binding will break when it is used with Each. Sample scripts as follows.

`SomeComponent.ux`

```auto
<Panel ux:Class="SomeComponent">

    <JavaScript>
    ... blah blah blah ...
    </JavaScript>

    <Text Value="{name}" />
</Panel>

```

`Main.ux`

```auto
<App>

    <Panel>
        <Each Items="{some_observable}">
            <SomeComponent />
        </Each>
    </Panel>

</App>

```

---

<div class="post-metadata">

**Author:** ![Edwin\_Reynoso](https://avatars.discourse-cdn.com/v4/letter/e/e99b99/32.png) [@Edwin\_Reynoso](https://forums.fusetools.com/u/Edwin_Reynoso)\
**Post date:** [March 11, 2016, 9:45am UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385/2 "2016-03-11T09:45:16Z")

</div>

I’m not sure how it’s breaking in your example, but I’m sure its supposed to since you for Every item in `some_observabl` you are re-executing the JS in the

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [March 11, 2016, 10:35am UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385/3 "2016-03-11T10:35:37Z")

</div>

Hi Vicker,

It is a little hard to make out what your problem is from your condensed snippet. Can you please provide a full test case?

When you have `JavaScript` inside an `Each`, the JS is evaluated for each item in the list, producing separate modules.

---

<div class="post-metadata">

**Author:** ![Vicker\_Leung](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/vicker_leung/32/392_2.png) [@Vicker\_Leung](https://forums.fusetools.com/u/Vicker_Leung)\
**Post date:** [March 15, 2016, 7:40am UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385/4 "2016-03-15T07:40:54Z")

</div>

Thanks, Edwin and Anders.

Let me try to extract the snippets into a small project to illustrate.

In the mean time, let me further elaborate a bit. Let’s say the some\_observable contains a list of 10 data objects. Without the JavaScript block in `SomeComponent.ux`, the data binding works correctly, displaying 10 items with the corresponding names.

However when there is a JavaScript block there, even the block itself contains nothing, it will break the data binding. i.e. it no longer shows any items as the `Text` is not able to bind `name`.

---

<div class="post-metadata">

**Author:** ![Vicker\_Leung](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/vicker_leung/32/392_2.png) [@Vicker\_Leung](https://forums.fusetools.com/u/Vicker_Leung)\
**Post date:** [March 22, 2016, 6:48am UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385/5 "2016-03-22T06:48:35Z")

</div>

This is a small project to illustrate the problem.

`MainView.ux`

```auto
<App Theme="Basic">

    <JavaScript>
        var data = ["A", "B", "C", "D", "E"];

        module.exports = {
            data: data
        };
    </JavaScript>

    <StackPanel>
        <Each Items="{data}">
            <Component />
        </Each>
    </StackPanel>

</App>

```

`Component.ux`

```auto
<Panel ux:Class="Component">

    <JavaScript>
    </JavaScript>

    <Text FontSize="13" Value="{}" />

</Panel>

```

When the app launch, it failed to display the data using Text, unless the empty JavaScript block is removed.

---

<div class="post-metadata">

**Author:** ![Anders\_Lassen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_lassen/32/522_2.png) [@Anders\_Lassen](https://forums.fusetools.com/u/Anders_Lassen)\
**Post date:** [March 30, 2016, 12:15pm UTC](https://forums.fusetools.com/t/javascript-inside-a-component-used-inside-each-breaks-data-binding/4385/6 "2016-03-30T12:15:54Z")

</div>

Vicker, yes, this is a known limitation.

The empty binding `{}` in your case now refer to the empty module “exported” from the empty JavaScript block.

A work-around is:

```auto
<App Theme="Basic">

    <JavaScript>
        var data = [{label: "A"}, {label: "B"}, {label: "C"}];

        module.exports = {
            data: data
        };
    </JavaScript>

...

```

And in `Component.ux`:

```auto
<Panel ux:Class="Component">

    <JavaScript>
    </JavaScript>

    <Text FontSize="13" Value="{label}" />

</Panel>

```

Does this solve your problem? If it does, what we need here is some form of syntax to indicate you want to access the parent data context, for example:

```auto
<Text FontSize="13" Value="{^}" />

```

(Not implemented yet, just a suggestion).
