<commit_before>
                                           |     |
    ,---..   .,---.,---.,---.,-.-.,---.,---|,---.|
    `---.|   ||   ||---'|    | | ||   ||   ||---'|
    `---'`---'|---'`---'`    ` ' '`---'`---'`---'`---'
              |


Supermodel is a small extension for tracking collections, models, and their
associations with [Backbonejs][backbone].

*Note: This is alpha software.  A similar version has been used at pathable for
quite some time but your milage may vary.*

# Model

`Supermodel.Model` is an extension of `Backbone.Model` that handles the
tracking and creation of individual models.


### Model Tracking

In large applications there are often multiple model objects representing the
same server object.  This can cause synchronization problems and cause the
display of stale data.  For instance,

```javascript
var user = new User({id: 5}).fetch();
...
var duplicate = new User({id: 5, name: 'brad'}).fetch();
user.get('name') == duplicate.get('name'); // false :(
```

If the server is updated while these models are being fetched the two instances
may have conflicting attributes.  To circumvent this, Supermodel tracks models
in the `all` property of the constructor (which is itself a collection) and
returns existing models instead of creating new ones when possible.

```javascript
var user = User.create({id: 5});
User.create({id: 5}) === user; // true
User.all.get(5) === user; // true
```


### Model.create

In order to track models, Supermodel needs to check for their existence before
returning a new model.  This is the job of `Model.create`.

```javascript
var User = Supermodel.Model.extend();
var user = User.create();
```

You should also use `Model.create` for the `model` property of your
collections.  This ensures that models created by your collections are
tracked and not duplicates.

```javascript
var Users = Backbone.Collection.extend({
  model: User.create
});
```

When using an existing model, it's assumed that the attributes provided are
newer than the existing attributes and they are updated.

```javascript
var user = User.create({id: 5, name: 'bradley'});
user = User.create({id: 5, name: 'brad'});
user.get('name'); // brad
```


### Model.all

Each model is stored in the `all` collection on the constructor for tracking
and event propagation.  These are rather handy for tracking events on an entire
collection.

```javascript
  var User = Supermodel.Model.extend();
  var user = User.create({id: 3});
  User.all.get(3) === user; // true
``

These also work for child constructors.

```javascript
  var Admin = User.extend();
  var admin = Admin.create({id: 2});
  Admin.all.get(2) === admin; // true
  User.all.get(2) === admin; // true
```

A few things to keep in mind about inheritance with `all` collections:

* All models with a common ancestor in their prototype chain
  (excluding `Supermodel.Model`) are assumed to have unique ids.
* Models should always be created with the most specialized constructor
  possible.  A model's super model can be deduced from the prototype chain, but<commit_msg>Add missing backtick.<commit_after>
                                           |     |
    ,---..   .,---.,---.,---.,-.-.,---.,---|,---.|
    `---.|   ||   ||---'|    | | ||   ||   ||---'|
    `---'`---'|---'`---'`    ` ' '`---'`---'`---'`---'
              |


Supermodel is a small extension for tracking collections, models, and their
associations with [Backbonejs][backbone].

*Note: This is alpha software.  A similar version has been used at pathable for
quite some time but your milage may vary.*

# Model

`Supermodel.Model` is an extension of `Backbone.Model` that handles the
tracking and creation of individual models.


### Model Tracking

In large applications there are often multiple model objects representing the
same server object.  This can cause synchronization problems and cause the
display of stale data.  For instance,

```javascript
var user = new User({id: 5}).fetch();
...
var duplicate = new User({id: 5, name: 'brad'}).fetch();
user.get('name') == duplicate.get('name'); // false :(
```

If the server is updated while these models are being fetched the two instances
may have conflicting attributes.  To circumvent this, Supermodel tracks models
in the `all` property of the constructor (which is itself a collection) and
returns existing models instead of creating new ones when possible.

```javascript
var user = User.create({id: 5});
User.create({id: 5}) === user; // true
User.all.get(5) === user; // true
```


### Model.create

In order to track models, Supermodel needs to check for their existence before
returning a new model.  This is the job of `Model.create`.

```javascript
var User = Supermodel.Model.extend();
var user = User.create();
```

You should also use `Model.create` for the `model` property of your
collections.  This ensures that models created by your collections are
tracked and not duplicates.

```javascript
var Users = Backbone.Collection.extend({
  model: User.create
});
```

When using an existing model, it's assumed that the attributes provided are
newer than the existing attributes and they are updated.

```javascript
var user = User.create({id: 5, name: 'bradley'});
user = User.create({id: 5, name: 'brad'});
user.get('name'); // brad
```


### Model.all

Each model is stored in the `all` collection on the constructor for tracking
and event propagation.  These are rather handy for tracking events on an entire
collection.

```javascript
  var User = Supermodel.Model.extend();
  var user = User.create({id: 3});
  User.all.get(3) === user; // true
```

These also work for child constructors.

```javascript
  var Admin = User.extend();
  var admin = Admin.create({id: 2});
  Admin.all.get(2) === admin; // true
  User.all.get(2) === admin; // true
```

A few things to keep in mind about inheritance with `all` collections:

* All models with a common ancestor in their prototype chain
  (excluding `Supermodel.Model`) are assumed to have unique ids.
* Models should always be created with the most specialized constructor
  possible.  A model's super model can be deduced from the prototype chain, but
{'fraction_non_alphanumeric': 0.1376281112737921, 'fraction_numerical': 0.003904343582235237, 'mean_word_length': 3.3479490806223478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1932043', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1687, 'n_words': 735}