dchacke / entangled-angular

Client side counterpart to Ruby gem "Entangled" for Angular
MIT License
2 stars 0 forks source link

How to call a custom action? #1

Closed Shigawire closed 9 years ago

Shigawire commented 9 years ago

In your backend code you provided the possibility to call custom actions additionally to the RESTful ones.

Let's assume I have the following code in my controller:

def start
 broadcast do
  @server = Server.find(params[:id]).start()
 end
end

How would I call the start action from this client side?

Good work by the way! I really enjoy not having to take care of the data being pulled into the frontend.

dchacke commented 9 years ago

What's your route to that action?

Shigawire commented 9 years ago

Like this

          servers_new GET      /servers/new(.:format)                              servers#new
       create_servers GET      /servers/create(.:format)                           servers#create
        update_server GET      /servers/:id/update(.:format)                       servers#update
       destroy_server GET      /servers/:id/destroy(.:format)                      servers#destroy
         start_server GET      /servers/:id/start(.:format)                        servers#start
          stop_server GET      /servers/:id/stop(.:format)                         servers#stop
              servers GET      /servers(.:format)                                  servers#index
               server GET      /servers/:id(.:format)                              servers#show

Edit: I've digged through the Javascript Code a bit, but could not find anything about anything other than CRUD.. The minified script made it a bit hard to inspect the code.

dchacke commented 9 years ago

I don't think there's currently a method baked into the Angular package. You can always instantiate your own socket though:

var socket = new WebSocket('ws://localhost:3000/servers/1/start');
socket.onopen = function (data) { console.log('opened:', data); }
socket.onmessage = function (data) { console.log('message:', data); }

Or something similar. Entangled should take care of the rest in the backend though. If it doesn't, let me know.

Shigawire commented 9 years ago

Well, I thought so. Thanks for clearing that up!

dchacke commented 9 years ago

Here's the un-minified source if you're interested.