NeutroniumCore / Neutronium

🚀 Build .NET desktop applications using HTML, CSS and javascript.
https://neutroniumcore.github.io/Neutronium/
MIT License
1.35k stars 122 forks source link

Execute RelayCommand from plain javascript #33

Closed nothing628 closed 6 years ago

nothing628 commented 6 years ago

I know it's not place to ask question, and this is not an issue. I already read your documentation about binding RelayCommand for VueJS. So, for execute the command come from C# is there two way in VueJS. <button v-command:"AddSkill"><\button> or <button @:click:"RemoveSkill.Execute(skill)"></button>

But, is possible to call RelayCommand from plain javacript? maybe like this : window.ViewModel.AddSkill();

I want to use Neutronium in my next project, and this not limited to the RelayCommand. Can get access the view model from plain javascript is big point. So my question just for the RelayCommand. Is there way to meet this point? or i just miss something on your documentation?

Thank you

David-Desmaisons commented 6 years ago

Hello @nothing628 , I assume that you will use Vuejs javascript framework integration. ICommand, ICommand, ICommandWithParameter, ISimpleCommand and ISimpleCommand are transformed into javascript object with an Execute method that has C# bindings.

As such you can call this method from javascript without problem. Then you have to get the application main view Model.

You can try to modify the vue application component to get a reference to it. Assuming you are using neutronium vue-cli you can do the following:

mounted: function () {
    window.ViewModel = this.ViewModel ;
}

Maybe the best place to continue this dicussion is on stackoverflow, you can use the hashtag Neutronium and I can give you more tips if needed,

nothing628 commented 6 years ago

I already try your code, but something is missing. The window.ViewModel after my inspection, the result is undefined. I think your code is trying to reference the this.ViewModel while the object not be able to access And i change your code like this :

    var that = this;
    setTimeout(() => {
      window.ViewModel = that.viewModel
    }, 1000)

The result is works. I seems there is a delay when injecting the viewmodel. But now the result is OK, i will close this issue