Skip to content

Commit c9f8323

Browse files
author
robin
committed
Fix readme and remove helpers from test suite
Ref T32139
1 parent dc3c38f commit c9f8323

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { Model } from 'mobx-spine';
4545

4646
// Define a class Animal, with 2 observed properties `id` and `name`.
4747
class Animal extends Model {
48-
@observable id = null; // Default value is null.
48+
@observable id; // Default value is undefined, a new negative integer will automatically be generated for a new model.
4949
@observable name = ''; // Default value is ''.
5050
}
5151
```
@@ -56,7 +56,7 @@ If we instantiate a new animal without arguments it will create an empty animal
5656
// Create an empty instance of an Animal.
5757
const lion = new Animal();
5858

59-
console.log(lion.id); // null
59+
console.log(lion.id); // -1, a unique negative integer for this model
6060
console.log(lion.name); // ''
6161
```
6262

@@ -67,6 +67,7 @@ You can also supply data when creating a new instance:
6767
const cat = new Animal({ id: 1, name: 'Cat' });
6868

6969
console.log(cat.name); // Cat
70+
console.log(cat.id); // 1
7071
```
7172

7273
When data is supplied in the constructor, these can be reset by calling `clear`:
@@ -90,6 +91,13 @@ cat.name = '';
9091
console.log(cat.undefinedProperty); // undefined
9192
```
9293

94+
### New models
95+
A new model is a model that exists in the store, but not on the backend. A new model either has a negative id, or the id is `null`. Checking if a model is new can be done with `model.isNew()` which returns a boolean `true` when the model is new.
96+
97+
By default, a new model will be initialized with a negative id, this way the model can be used as a related model. When a model is initialized with a negative id it will automatically generate a new negative id for the model on a clear. In some cases a `null` id might be preferred, in this case a model can be forced to get a `null` id by passing `{id: null}` in the constructor. In this case the id will also be reset to `null` on a clear. A model with a `null` id functions the same as a model with a negative id other than that the model cannot be used in a relation.
98+
99+
Some projects might still use the legacy method of checking for new models by checking if `!model.id`. This does not work with the default negative IDs. To migrate a project to the default negative IDs implementation you should search and replace the whole project for occurrences of `.id` and fix all lines that check if an id is set to use the `isNew()` property.
100+
93101
### Constructor: options
94102

95103
|key|default| | |
@@ -308,6 +316,9 @@ class animal = new Animal({ id: 2, name: 'Rova', breed: { id: 3, name: 'Main Coo
308316
console.log(animal.breed.name); // Throws cannot read property name from undefined.
309317
```
310318

319+
### Negative IDs for related models
320+
A related model will always be initialized with a `null` id. When the id of the related model is set to `null` it indicates to django-binder that the field is empty.
321+
311322
### Pick fields
312323

313324
You can pick fields by either defining a static `pickFields` variable or a `pickFields` function. Keep in mind that `id` is mandatory, so it will always be included.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"./src"
7474
],
7575
"testPathIgnorePatterns": [
76-
"/fixtures/"
76+
"/fixtures/",
77+
"/helpers.js"
7778
]
7879
}
7980
}

src/__tests__/Model.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ test('Initialize model with invalid data', () => {
6666
test('Initialize model without data', () => {
6767
const animal = new Animal(null);
6868

69-
expect(animal.id).toBeNull();
69+
expect(animal.id).toBeLessThan(0);
7070
expect(animal.name).toBe('');
7171
});
7272

@@ -1437,7 +1437,7 @@ describe('requests', () => {
14371437
return [201, animalMultiPutResponse];
14381438
});
14391439

1440-
return animal.validateAll({ relations: ['kind'] }).then(response => {
1440+
return animal.validate({ relations: ['kind'] }).then(response => {
14411441
expect(spy).not.toHaveBeenCalled();
14421442
expect(animal.id).toBe(10);
14431443
expect(animal.kind.id).toBe(4);

0 commit comments

Comments
 (0)