dsuryd / dotNetify

Simple, lightweight, yet powerful way to build real-time web apps.
https://dotnetify.net
Other
1.17k stars 164 forks source link

Cannot call vm.$dispatch for Vue #139

Closed Simcon closed 5 years ago

Simcon commented 5 years ago

I've taken the Vue From Scratch example and I'm trying to get the button click to work as shown in http://www.dotnetify.net/core/examples/controltypes,

In the browser console I get a 'vm.$dispatch is not a function' message.

I'm wondering if it's because I'm outside of a vue template (similar to https://github.com/dsuryd/dotNetify/issues/136). If so, how can I make this work?

<html>
  <head>
    <title>DotNetify</title>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, width=device-width" />
  </head>
  <body>
    <div id="App">
        <div>
            <p>{{Greetings}}</p>
            <p>Server time is: {{ServerTime}}</p>
            <p><button class="btn btn-secondary" type="button" @click="onButtonClick">Click me</button></p>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script src="https://cdn.jsdelivr.net/npm/@aspnet/signalr@1/dist/browser/signalr.min.js"></script>
    <script src="https://unpkg.com/dotnetify@3/dist/dotnetify-vue.min.js"></script>

    <script>
      vm = new Vue({
        el: '#App',
        created: function() { dotnetify.vue.connect("HelloWorld", this) },
        data: { Greetings: '', ServerTime: '' },
        methods: {
            onButtonClick: function () {
              vm.$dispatch({ ButtonClicked: true });
            }        
        }
      })
    </script>
  </body>
</html>
using System;
using DotNetify;
using System.Threading;

namespace HelloWorld
{
    public class HelloWorld : BaseVM
    {
        private Timer _timer;
        public string Greetings => "Hello World!";
        public DateTime ServerTime => DateTime.Now;

        public Action<bool> ButtonClicked => _ => 
        {
            Console.WriteLine("CLICK");
        };

        public HelloWorld()
        {
            Console.WriteLine("START");
            _timer = new Timer(state =>
            {
                Changed(nameof(ServerTime));
                PushUpdates();
            }, null, 0, 1000);
        }

        public override void Dispose() => _timer.Dispose();
    }
}
dsuryd commented 5 years ago

The vm is dotnetify’s and not Vue’s:

this.vm = dotnetify.vue.connect(...
...
this.vm.$dispatch(...
Simcon commented 5 years ago

Of course! Well that's a little embarrassing. Confirmed working - thanks!