You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-2Lines changed: 13 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,7 @@ import { Model } from 'mobx-spine';
45
45
46
46
// Define a class Animal, with 2 observed properties `id` and `name`.
47
47
classAnimalextendsModel {
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.
49
49
@observable name =''; // Default value is ''.
50
50
}
51
51
```
@@ -56,7 +56,7 @@ If we instantiate a new animal without arguments it will create an empty animal
56
56
// Create an empty instance of an Animal.
57
57
constlion=newAnimal();
58
58
59
-
console.log(lion.id); //null
59
+
console.log(lion.id); //-1, a unique negative integer for this model
60
60
console.log(lion.name); // ''
61
61
```
62
62
@@ -67,6 +67,7 @@ You can also supply data when creating a new instance:
67
67
constcat=newAnimal({ id:1, name:'Cat' });
68
68
69
69
console.log(cat.name); // Cat
70
+
console.log(cat.id); // 1
70
71
```
71
72
72
73
When data is supplied in the constructor, these can be reset by calling `clear`:
@@ -90,6 +91,13 @@ cat.name = '';
90
91
console.log(cat.undefinedProperty); // undefined
91
92
```
92
93
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
+
93
101
### Constructor: options
94
102
95
103
|key|default|||
@@ -308,6 +316,9 @@ class animal = new Animal({ id: 2, name: 'Rova', breed: { id: 3, name: 'Main Coo
308
316
console.log(animal.breed.name); // Throws cannot read property name from undefined.
309
317
```
310
318
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
+
311
322
### Pick fields
312
323
313
324
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.
0 commit comments