Reputation: 100210
I am using MongoDB with Node.js on the backend and Backone on the front-end. Backbone models have unique ids (cid's and idAttribute) that can identify the models on the backend. But Backbone collections do not have a unique identifier to my knowledge, and that is one reason why I ask the question that emerges from the remainder of the text below:
In MongoDB, collections have names as strings, which represent their unique identifier. What is the best way to match the collection string name in MongoDB with some sort of unique string name for a Backbone collection on the front-end?
Do Backbone collections have some sort of unique identifier? What is the best way to create one?
The reason why I ask is because normally we match Backbone collections with the backend using the URL property of the collection. However, I am starting to incorporate sockets (socket.io) in my application. It goes like this: I have a secondary server (separate from the application server) that listens to the MongoDB oplog. Upon an insert/update/delete event in the oplog, the secondary server sends a socket message to my front-end with BSON/JSON information that contains: <dbname>.<collectionname>
. So something like: mainDB.users_collection
So on my front-end, I need to match the Backbone collection with the literal string representation of the MongoDB collection. Now, I could just set a property on the Backbone collection, say, "uniqueName" but I am wondering if there is a better practice. Someone has done this before and I want to learn from them. Good grief.
Upvotes: 0
Views: 76
Reputation: 17878
The most common way of integrating Backbone with sockets is to change the Backbone.Sync
object. The default Sync assumes a traditional REST API and is meant to be changed when a different backend is used.
Alternatively, you can use an existing plugin, like https://noveogroup.github.io/backbone.iobind/
Upvotes: 1