entangled icon indicating copy to clipboard operation
entangled copied to clipboard

how to send custom action result to the client

Open ffabreti opened this issue 8 years ago • 1 comments

Hi Dennis, In the docs, you say:

The index action will expect an instance variable with the same name as your controller in the plural form (e.g. @messages in a MessagesController) The show, create, update, and destroy actions will expect an instance variable with the singular name of your controller (e.g. @message in a MessagesController)

What about custom actions? How do I return a collection to the client?

With the following code, I get nothing on client: Rails:

    def since
        broadcast do
            since = chat_room_since
            @chat_rooms = []
            @chat_rooms = ChatRoom.where("last_time > ?", since).to_a if since
        end
    end

    private
    def chat_room_since
        date = URI.decode params.require(:date)
        DateTime.parse(date) if date
    end

Angular:

since: function(dateString, cback) {

        if (!sinceSocket) {
            sinceSocket = new WebSocket(BASEURL + '/chat_rooms/since');
            sinceSocket.onopen = function (data) {
                sinceSocket.send(JSON.stringify({date: dateString}));
            }
        }
        sinceSocket.onmessage = function (data) {
            if (data.err) {
                console.log('Chatrooms: create: error: ',err);
            }
            if (cback) cback(data);

        };

     },

ffabreti avatar Apr 09 '16 20:04 ffabreti

Confirmed. It seems there is an issue with https://github.com/dchacke/entangled/blob/development/lib/entangled/controller.rb#L214, where tubesock.send_data never ends up getting called. This is where all instance variables should be sent.

Currently swamped but will address this asap. In the meantime, I suggest the following workaround: Create another controller and use its index action for your purposes instead.

A known issue (or rather, a missing feature) is proper scoping through where clauses. This may come up in the workaround.

dchacke avatar Apr 18 '16 05:04 dchacke