jkonowitch / ss-backbone

SocketStream Backbone backend over socket.io
29 stars 6 forks source link

support for custom model 'id' attribute #2

Open nhitchins opened 11 years ago

nhitchins commented 11 years ago

A useful enhancement would be to allow for the model 'id' attribute to be specifed as per: http://backbonejs.org/#Model-idAttribute

then in the ss-backbone client.js file modify any direct reference to 'id'to use the model class idAttribute: registerModel(model, modelname, attrs[this.idAttribute] || model.cid);

obviously you just need to check the property exists otherwise set it's default to 'id'

davisford commented 11 years ago

I need this too, as I'm using mongo backed storage with "_id" attributes, and I just added the property

MyModel = syncedModel.extend({
   idAttribute: "_id"
}, {
   modelName: "MyModel"
});

This seems to be working -- am I missing something?

nhitchins commented 11 years ago

Davis,

The client.js file in the ss-backbone libs has this in the Initialization method (line ~50):

registerModel(model, modelname, attrs.id || model.cid);

I've replaced it with:

this.idAttribute = this.idAttribute || 'id'; registerModel(model, modelname, attrs[this.idAttribute] || model.cid);

There are also a few other places in that same client.js file that referene the .id attribute which i've just replaced with [this.idAttribute]

jkonowitch commented 11 years ago

@nhitchins ,

Apologies for the late response here. Nice solution - feel free to submit a pull request and I'll merge in the change.