In "old" enyo for HPalm devices I was able to call my enyo kind functions from/via JS like this:
<span class="button" onclick="app.doSomething();"></span>
Now in enyoJS it doesn't work. My app.js looks like this:
enyo.kind({
name: "myapp.Application",
kind: "enyo.Application",
view: "myapp.MainView"
});
enyo.ready(function () {
new myapp.Application({name: "app"});
});
I tried pretty much everything I could think of, but nothing seams to do the trick anymore.
var app = new myapp.MainView();
app.renderInto(document.body);
app.doSomething();
^ etc does not work either.
My MainView:
enyo.kind({
name: "myapp.MainView",
kind: "FittableRows",
classes: "enyo-fit enyo-unselectable",
fit: true,
components:[
//... stuff here ...
],
doSomething: function(){
console.log("Hello!");
}
});
Is there a way how to achieve what I need? TYVM
You're on the right track. In fact, your attempt to create MainView directly works.
The reason app.doSomething()
doesn't work is:
There are a couple ways to get a reference to your Application instance. The easiest is to assign it to a global on create:
var app;
enyo.ready(function() {
app = new myapp.Application({name: "app"});
});
// later
app.doSomething();
Another option is to grab it from the enyo.applications
hash which stores a reference to each created application by its name.
enyo.applications.app.doSomething();
Here's a complete example
enyo.kind({
name: "myapp.Application",
kind: "enyo.Application",
view: "myapp.MainView",
doSomething: function() {
this.$.mainView.doSomething();
}
});
enyo.kind({
name: "myapp.MainView",
kind: "FittableRows",
classes: "enyo-fit enyo-unselectable",
fit: true,
components:[
//... stuff here ...
],
doSomething: function(){
console.log("Hello!");
}
});
enyo.ready(function () {
var app = new myapp.Application({name: "app"});
// works, but not recommended as it breaks encapsulation
// app.$.mainView.doSomething();
// preferred but requires a little extra code
app.doSomething();
});