Models

AJAX Abstraction for a Single Resource

generateCid

The generateCid() method generates the cid value for the model. The default behavior is to generate a string value that looks like c1, c2, etc.

Default Implementation

The default implementation looks like this:

import _ from 'lodash';
...
generateCid: function() {
  return _.uniqueId(this.cidPrefix);
},

The uniqueId() method belogns to lodash, and you can find documentation for it here.

Usage

The default behavior for models generates cid values that look like c1, c2, etc. Take a look at this code to illustrate:

import { Model } from 'lore-models';

const Tweet = Model.extend();

const t1 = new Tweet();
const t2 = new Tweet();

In the example above, t1 will have a cid of c1, and t2 will have a cid of c2.

There are times however when you might want to completely change how a cid is generated. For example, when performing optimistic updates using websockets, you need to generate a unique id on the client side, and then send that id to the server. In that case, the value needs to be unique across all clients, and not just unique for a single user.

For example, let's say we wanted to generate a UUID as value for each cid, to guarantee it was globally unique. We could do that like this:

import { Model } from 'lore-models';
import uuid from 'node-uuid';

const Tweet = Model.extend({
  generateCid: function() {
    return uuid.v4();
  }
});

const t1 = new Tweet();
const t2 = new Tweet();

Now t1 will have a cid like 088025da-4adc-4ada-9103-aae0660e522a, and t2 will have a cid like 0ea5059c-8c92-4406-bf74-3c87ccdc5199.