scttnlsn / backbone.io

Backbone.js sync via Socket.IO
http://scttnlsn.github.io/backbone.io
541 stars 66 forks source link

In 'Backbone.Collection' should I Use 'Backend:' or 'URL:'? #53

Closed 1manStartup closed 10 years ago

1manStartup commented 10 years ago

So im trying to connect my Database created JSON REST API to Backbone.IO.

1) Normally a Backbone Collection would be like this...

Messages = Backbone.Collection.extend({ model: Message, url: '/api/messages' });

2) But I notice that Backbone.IO uses a 'backend' property. Am I supposed to add 'backend' with 'url' like so (The goal is to use with my api)?

var Messages = Backbone.Collection.extend({ // Specify the backend with which to sync backend: 'messages', //URL from API here url: '/api/messages', model: Message, initialize: function() { // Setup default backend bindings // (see lib/browser.js for details). this.bindBackend(); } });

Thanks.

scttnlsn commented 10 years ago

The url is for syncing data via AJAX requests. When backend is specified, Backbone.IO overrides the default implementation of Backbone.sync to sync models over a Socket.IO connection. If you have a REST API but want to push updates to the client I would recommend something lower-level like https://github.com/logicalparadox/backbone.iobind or just plain Socket.IO. If you're just trying to push updates from the server and use AJAX for all CRUD operations I usually prefer something simpler like server-sent events (http://www.html5rocks.com/en/tutorials/eventsource/basics/).

1manStartup commented 10 years ago

Got it, will check those out. Thanks.