ianhaggerty / backbone-sails

A plugin for Backbone that makes integrating with Sails JS easier. Both leverage a model-view architecture, it's only natural to have them talk to each other.
MIT License
7 stars 1 forks source link

Can't get socket events working. #2

Closed dottodot closed 9 years ago

dottodot commented 9 years ago

Hopefully this will be the last of my getting start questions but I can't seem to get any socket events working. I've looked at your example app and can't see the difference that's stopping it working.

In the example below I would expect that once a new list item is saved the "created" event would fire on the lists collection, but nothing is happening.

The only thing I've noticed but not sure if it's related is undefined showing at the end of my socket request i.e.

socket request: get /api/lists?where={"team":"533951b78972167e2d3c5d10"}&limit=30&watch=true undefined 
var lists = new App.Entities.ListCollection;
return lists.query({
  where: {
    team: team.id
  }
}).fetch({
  socketSync: true
}).then(function() {

  var listsListView = new View.Lists({
    collection: lists
  });

  listsListLayout.on("show", function() {
    listsListLayout.panelRegion.show(listsListPanel);
    listsListLayout.listsRegion.show(listsListView);
  });

  listsListPanel.on("list:new", function() {
    require(["apps/lists/new/new_view"], function(NewView) {

      var newList = new App.Entities.List({
        team: team.id
      });

      var view = new NewView.List({
        model: newList
      });

      view.on("form:submit", function(data) {
        newList.save(data).done(function() {
          lists.add(newList);
          view.trigger("modal:close");
          var newListView = listsListView.children.findByModel(newList);
          if (newListView) {
            newListView.flash("success");
          }
        });
      });

      App.modalRegion.show(view);
    });
  });

  App.sideRegion.show(listsListLayout);

  return lists.on("created", function(data) {
    console.log('list created');
    var list = new App.Entities.List(data);
    return list.fetch().done(function() {
      lists.add(list);
      var newListView = listsListView.children.findByModel(list);
      if (newListView) {
        newListView.flash("success");
      }
    });
  });
});
ianhaggerty commented 9 years ago

Hello.

I think this is the problem:

.fetch({
  socketSync: true
})

This is actually an old version of the sync configuration. You can simply use a single sync option taking a space delimited set of flags:

.fetch({
  sync: 'socket'
})

I would suggest reading the syncing tutorial.

You might try explicitly setting the autowatch configuration option on the server side blueprints.

Have you included the blueprints under api/blueprints?

dottodot commented 9 years ago

Odd, I've also tried

var lists = new App.Entities.ListCollection;
            return lists.query({
              where: {
                team: team.id
              }
            }).fetch({
             sync: 'socket'
            }).then(function() {

              lists.on('created', function(data) {
                console.log(data);
              });
            });

and I already had autowatch on as I'd noticed it in your example app.

ianhaggerty commented 9 years ago

There is a sails option known as mirror for the blueprints. It is as of yet undocumented but it is massively important if you want socket events to come back to the client they originate from.

I've made a gripe about it here.

Try setting that to true.

dottodot commented 9 years ago

It wasn't that either. I had added prefix in my blueprint of prefix: '/api' and on my model was using `modelName: "api/list"``

This works fine for fetching saving etc, but not with socket events.

ianhaggerty commented 9 years ago

Ah ok. There is a configuration option dedicated to that.

You'll need to set your model name to list and then configure the prefix option globally:

Backbone.Sails.configure({
  prefix: '/api'
})

List = Backbone.Sails.Model.extend({
  modelName: 'list'
})

I have just ran the tests with this and is all working fine :)

dottodot commented 9 years ago

Sorry had completely missed that, all working now.