# function re-call from js

**URL:** <https://forums.fusetools.com/t/function-re-call-from-js/2420>\
**Category:** How-to Discussions\
**Created:** [October 14, 2017, 11:52am UTC](https://forums.fusetools.com/t/function-re-call-from-js/2420 "2017-10-14T11:52:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hendrik](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@Hendrik](https://forums.fusetools.com/u/Hendrik)\
**Post date:** [October 14, 2017, 11:52am UTC](https://forums.fusetools.com/t/function-re-call-from-js/2420/1 "2017-10-14T11:52:40Z")

</div>

Hi,

I have a problem, when calling a function from a.js in b.js that then calls a function in a.js. Heres the example:

testapp2.unoproj

```auto
{
  "RootNamespace":"",
  "Packages": [
    "Fuse",
    "FuseJS"
  ],
  "Includes": [
    "*",
    "*.js:Bundle"
  ]
}

```

Mainview.ux:

```auto
<App>
	<JavaScript File="b.js" />
	<JavaScript File="a.js" />

	<Button Text="fooa" Clicked="{fooa}" />
</App>

```

a.js:

```auto
b = require('b');

function fooa(){
	b.foob(); // calling the function from ux works without error
}

function bara(){
	console.log("bar a")
}

b.foob(); // this is what causes the error

module.exports = {
	fooa: fooa,
	bara: bara
}

```

b.js:

```auto
a = require('a');

function foob(){
	a.bara(a);
}

module.exports = {
	foob: foob
}

```

If I comment out the b.barb() in a.js, I can press the ux button and everything works fine, but if call the function directly from a.js, then I get this error:

```auto
Error: TypeError: a.bara is not a function: Name: TypeError: a.bara is not a function
Error message: Uncaught TypeError: a.bara is not a function
File name: b.js
Line number: 4
Source line: a.bara(a);
JS stack trace: TypeError: a.bara is not a function
    at Object.foob (b.js:4:4)
    at a.js:11:3
    at Object._tempMethod (b.js:1:53)
 in Fuse.Reactive.DiagnosticSubject<b.js:4>
Error: TypeError: a.bara is not a function: Name: TypeError: a.bara is not a function
Error message: Uncaught TypeError: a.bara is not a function
File name: b.js
Line number: 4
Source line: a.bara(a);
JS stack trace: TypeError: a.bara is not a function
    at Object.foob (b.js:4:4)
    at Object._tempMethod (a.js:11:3)
 in Fuse.Reactive.DiagnosticSubject<b.js:4>

```

So why can I call it through ux, but not from js and how can I get it working from js?

---

<div class="post-metadata">

**Author:** ![Hendrik](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@Hendrik](https://forums.fusetools.com/u/Hendrik)\
**Post date:** [October 14, 2017, 2:50pm UTC](https://forums.fusetools.com/t/function-re-call-from-js/2420/2 "2017-10-14T14:50:52Z")

</div>

I found a workaround, but its not really satisfying:

I use a switch to trigger a callback in the ux from js:

a.js:

```auto
var Observable = require('FuseJS/Observable');

b = require('b');

var callfromui = Observable(false);

function fooa(){
	b.foob();
	callfromui.value = false;
}

function bara(){
	console.log("bar a")
}

callfromui.value = true;

module.exports = {
	fooa: fooa,
	bara: bara,
	callfromui: callfromui
}

```

Mainview.ux:

```auto
<App>
	<JavaScript File="b.js" />
	<JavaScript File="a.js" />

	<WhileTrue Value="{callfromui}">
    	   <Callback Handler="{fooa}" />
	</WhileTrue>

	<Button Text="fooa" Clicked="{fooa}" />
</App>

```

It works, but I hope there is a better more comfortable way to do it.

---

<div class="post-metadata">

**Author:** ![Uldis](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/uldis/32/657_2.png) [@Uldis](https://forums.fusetools.com/u/Uldis)\
**Post date:** [October 16, 2017, 10:02am UTC](https://forums.fusetools.com/t/function-re-call-from-js/2420/3 "2017-10-16T10:02:58Z")

</div>

Hi,

the root cause of the problem you’re facing is right there:

```auto
<App>
    <JavaScript File="b.js" />
    <JavaScript File="a.js" />

```

A single UX component can only have a single viewmodel (the JS data context). Here, you have two. That won’t work.

Please [start reading here](https://www.fusetools.com/community/forums/howto_discussions/global_observable_from_parent_to_children?page=1&highlight=168b12b0-bf30-43c3-a561-8c38ad2eb0df#post-168b12b0-bf30-43c3-a561-8c38ad2eb0df) and follow the links to understand the concept and we can take it from there. You’ll find some more tips if you [search the forum for MVVM](https://www.fusetools.com/search?query=mvvm&type=forum_post).

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Hendrik](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@Hendrik](https://forums.fusetools.com/u/Hendrik)\
**Post date:** [October 16, 2017, 10:21am UTC](https://forums.fusetools.com/t/function-re-call-from-js/2420/4 "2017-10-16T10:21:25Z")

</div>

Hi Uldis,

thanks for the reply. These links helped and I splitted things up in a proper viewmodel and model structure. But the problem still exists, that I cannot re-call the functions between 2 models (non-viewmodel). In this example a.js and b.js would be models and there would actually be no viewmodel necessary for this example. But I will re-read and try to find out what I am missing. For now I solved it by outsourcing the bara function into a third model, so no model is calling itself through another model.

---

<div class="post-metadata">

**Author:** ![Uldis](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/uldis/32/657_2.png) [@Uldis](https://forums.fusetools.com/u/Uldis)\
**Post date:** [October 16, 2017, 10:25am UTC](https://forums.fusetools.com/t/function-re-call-from-js/2420/5 "2017-10-16T10:25:13Z")

</div>

> **Hendrik wrote:**  
> […] no model is calling itself through another model.

That’s the part I did not want to start explaining before you got the MVVM basics down 🙂

A given model “A” should never `require()` another model “B” that `require()`s the model “A”. The relationship between models is meant to be exclusive.
