I have a rest API which is able to produce objects keys with null value.
This null value are not showen in the model.attributes.
Here an example:
the server can produce {id: 1, attr: "someValue"} or {id: 2, attr: null}
Now If I run the following code all things are ok:
m = new MyModel({id: 1});
m.fetch();
m.get("attr"); // "bar";
m = new MyModel({id: 2});
m.fetch();
m.get("attr"); // undefined;
If I run the following I have a problem with the render function in the View.
To understand why, please look this code:
m = new MyModel({id: 1});
m.fetch({
success: function () {
m.get("attr"); // "bar";
}
});
m = new MyModel({id: 2});
m.fetch({
success: function () {
m.get("attr"); // "bar"; // actually it should be undefined or null
}
});
My questions are:
1) Why I get this behaviour?
2) How can I fix it?
Okay… I see what’s going on… In the first example, you’re setting var m to a new model instance. In the second example, you’re not resetting m to a new model, just setting the id value – it’s still the first model. That’s why you’re not seeing a change to “null.”