huafu / ember-data-sails

Adapters and tools for Ember to work well with Sails
MIT License
61 stars 31 forks source link

documentation #36

Open fridaystreet opened 8 years ago

fridaystreet commented 8 years ago

Hi,

I have a very simple model (one attribute) just for test purposes and I can see the initial socket connection happening and on my sails console in silly log mode it registers all the subscriptions before sending back the data in the model. Ember receives the data fine, but if I create a new record in sails or update a record (via say postman direct to the API) nothing shows up in my ember client. Also I can see in sails log that it has published the event when I update a record.

I've gone over and over the documentation, but I can't really see how I should be registering to listen for events. I think there is mention somewhere of being able to observe the events. I'm likely wrong, but I kind of got the impression you just need to do a store.find and return your model and the socket adapter just listens from that, but that doesn't seem to be happening.

I've read through all of the issues open and closed to see if there are any code snippets that could shed some light. I think one mentions something about computed properties, I've tried things like .on('interaction.update') .on('interaction.create') .observes('interaction.update') etc, but nothing seems to make any difference.

I tried this.sailsSocket.listenFor('interaction.update', true); which showed up in the console as [ed-sails] attached event interaction.update on socket

but still nothing when I update or create a record. So I'm just wondering is there something I'm missing in regards to enabling the listener?

Thanks in advance, any assistance is greatly appreciated.

Logs and details below

model interaction export default DS.Model.extend({ name : DS.attr('string') });

ember-data-sails log when route loads [ed-sails] socket not connected, listening for connect event before giving it ember.debug.js:258 [ed-sails] socket core object ready ember.debug.js:258 [ed-sails] triggering event didInitialize |> Now connected to Sails. ___/ For help, see: http://bit.ly/1DmTvgK (using browser SDK @v0.11.0)

ember.debug.js:258 [ed-sails] triggering event didConnect ember.debug.js:258 [ed-sails] socket GET request on /api/v1/interactions: SUCCESS ember.debug.js:258 [ed-sails] → request: undefined ember.debug.js:258 [ed-sails] ← response: Object {interactions: Array[29]}

ember route loading calling model in sails server | Fri, 04 Dec 2015 01:46:27 GMT: verbose: Receiving incoming message from Socket.io: { method: 'get', server | Fri, 04 Dec 2015 01:46:27 GMT: verbose: Interpreting socket.io message as virtual request to "get /api/v1/interactions"... server | Fri, 04 Dec 2015 01:46:27 GMT: silly: Handling virtual request :: Running virtual querystring parser... server | Fri, 04 Dec 2015 01:46:27 GMT: silly: Handling virtual request :: Running virtual body parser... server | Fri, 04 Dec 2015 01:46:27 GMT: verbose: -> GET undefined server | executing query against db frontend: SELECT name, @rid, createdAt, updatedAt FROM interaction LIMIT 500 SKIP 0 server | sending operation command for database frontend client | POST /socket.io/?sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=browser&sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1449193587343-2&sid=dR2dsPBO2yHNz_5sAAAJ 200 7.360 ms - 2 server | Fri, 04 Dec 2015 01:46:27 GMT: silly: Subscribed to the Interaction with id=#63:13 (room :: sails_modelinteraction#63:13:update)

update to record via postman call direct to api server | Fri, 04 Dec 2015 01:59:51 GMT: verbose: -> PUT /api/v1/interactions/%2363%3A13 server | executing query against db frontend: SELECT name, @rid, createdAt, updatedAt FROM interaction WHERE @rid = :param0 LIMIT 1 server | sending operation command for database frontend server | executing query against db frontend: UPDATE interaction SET updatedAt = :paramupdatedAt0 RETURN AFTER WHERE @rid = :param0 server | sending operation command for database frontend server | Fri, 04 Dec 2015 01:59:51 GMT: silly: Published interaction to sails_modelinteraction#63:13:update server | executing query against db frontend: SELECT name, @rid, createdAt, updatedAt FROM interaction WHERE @rid = :param0 LIMIT 1 server | sending operation command for database frontend server | Fri, 04 Dec 2015 01:59:51 GMT: silly: res.ok() :: Sending 200 ("OK") response server | Fri, 04 Dec 2015 01:59:51 GMT: verbose: -> PUT /api/v1/interactions/%2363%3A13 200 ( -> NaNms, 135 B)

simonporter007 commented 8 years ago

I'm in the same place, I've got the initial get request fired off and can see the socket connected in the console log, but I can't see the subscription being logged even if I see it hitting the subscribe action in my sails controller.

Consequently, updates and inserts don't seem to make it back to Ember.

Did you get to the bottom of it, find any good examples anywhere?

simonporter007 commented 8 years ago

For what it's worth, I had to use @danshapir fork where he included Pull Requests for Ember 2.0. I had to then modify some extra lines for 2.3.

1) Change the store lookup from using container to getOwner using plugin: import getOwner from 'ember-getowner-polyfill';

_listenToSocket: function (model) {
    var store, type;
    var eventName = camelize(model).toLowerCase();
    var socket = this.sailsSocket;
    if (socket.listenFor(eventName, true)) {
      this.notice(`setting up adapter to listen for '${model}' messages`);
      //store =this.container.lookup('store:main');
      store = getOwner(this).lookup('service:store');
      type = store.modelFor(model);
      socket.on(eventName + '.created', bind(this, '_handleSocketRecordCreated', store, type));
      socket.on(eventName + '.updated', bind(this, '_handleSocketRecordUpdated', store, type));
      socket.on(eventName + '.destroyed', bind(this, '_handleSocketRecordDeleted', store, type));
    }
  },

2) Changed the type to dasherized. _handleSocketRecordCreated in sails-socket.js

_handleSocketRecordCreated: function (store, type, message) {
    var record = message.data, payload = {};
    if (!record.id && message.id) {
      record.id = message.id;
    }
    payload[pluralize(camelize(type.modelName))] = [record];
    type = EmberString.dasherize(type.modelName);
    store.pushPayload(type, payload);
  },

Doesn't looks like there's much activity on the project, so I leave this here for searches.