Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Updated backbone-to-sails to be compatible with Sails v0.10 #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

aato
Copy link

@aato aato commented Dec 2, 2014

The biggest change was adding the ability to listen to messages sent from a specific Model (as opposed to interpreting all incoming messages in the 'comet' event). For example, to listen for messages from Twinkie:

Backbone.on('twinkie', function ( message ) {
    // Do things with message.
});

'comet' messages are still available and are the default if no socket message callbacks are supplied.

The only bit of visible overhead that's been added is the initBackbone() function which must be called AFTER the socket connection is made and BEFORE you begin working with Backbone. Here's an example:

socket.on('connect', function socketReady() {
    initBackbone(function() {
        // Safe to start working with Backbone.
    });
});

In order to listen for messages about a specific Model, you pass in an Object as the first argument to initBackbone() that maps Model names with callback functions that trigger a Backbone event:

socket.on('connect', function socketReady() {
    initBackbone({
        twinkie : function(msg)
        {
           Backbone.trigger('twinkie', msg);
        }
    },
    function() {
        // Safe to start working with Backbone.
    });
});

It's not the most elegant way to go about it but I haven't found a better way that doesn't rely on generating functions from strings using eval(). For example, it would be possible for initBackbone() to accept an array of event names:

socket.on('connect', function socketReady() {
    initBackbone(['twinkie'], function() {
        // Safe to start working with Backbone.
    });
});

which is much easier on the eyes but it would rely on using:

var eventName = "twinkie";
var code = "Backbone.trigger('" + eventName + "', msg);"
var cb = Function('msg', code);

in the background. If anyone knows of a better looking way to accomplish this I'd love to hear it. Let me know what you all think!

@aato
Copy link
Author

aato commented Dec 2, 2014

Also this build is most-likely failing because of a missing .travis.yml file.

@AlinaNova21
Copy link

This creates a callback without any messy evals. And, since evt is local to makeCallback, its only accessible from the returned function and remains the same for that function instance.

function makeCallback(evt){ 
    return function(msg){ Backbone.trigger(evt,msg) };
})
var cb = makeCallback("twinkie");

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants