New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
can/observe/attributes plugin does not properly inherit the attributes property #358
Comments
It used to be this way (likely JMVC 3.0), but we changed it because it was very hard to turn off attributes in inheriting constructor functions. |
Not 'very hard' at all: From the looks of things, the only required change to the can/observe/attributes plugin is that the default type converter act as a direct passthrough when its The above change would finish completely wiring up the plugin to ignore a null value for an attribute Provided that the above holds, a very simple pattern allows you to reset the converter and serializer for an attribute by passing var BaseObserve = can.Observe({
attributes : { "value" : "boolean" }
})
var InheritedObserve = BaseObserve({
attributes : { "value" : null }
});
var base = new BaseObserve({ "value" : "false" });
console.log( typeof base.value ); // "boolean"
var inh = new InheritedObserve({ "value" : "false" });
console.log( typeof inh.value ); // "string" instead of "boolean" Sure, you won't be able to get rid of the attribute entirely, but resetting its converter and serializer effectively does the same thing. All data members, also those not marked up explicitly as attributes, are still made observable by From a less pragmatic and more theoretical standpoint, some thoughts you may want to consider are:
*) sound polymorphism goes out the door, for one |
Define will inherit by default. Use that in 2.1 going forward. |
See: https://github.com/bitovi/canjs/blob/1aac092f5df252582d8318df0d6d2ef1707088d9/observe/attributes/attributes.js#L213-L223
Currently the
attributes
property is not being merged during inheritance, whereas theconvert
andserialize
property are.Having the attributes collection not inherit over flies in the face of OO design; if you create an inherited model, you want it to inherit from its base. You don't want to re-specify the core of the class, i.e. , the attributes and their type mappings, every time you create a layer of inheritance.
Introducing this rift in behavior also gives rise to a very subtle class of application bug where converters for attributes mysteriously 'go missing', with no obvious reason as to the reason. After all; the
convert
andserialize
members are properly inherited and present, and all the attributes will be present as well (thanks to the default behavior ofcan.Observe
processing and observing all child properties on the passed in data object). However, as the attribute mapping is not inherited, the calls to the converters simply never happen.If this inheritance behavior is by design, then it is a very poor design choice.
The text was updated successfully, but these errors were encountered: