Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Possible bug? Using arrays/objects in class definitions

    What is your opinion about defining arrays or objects as class properties? I always set these in the init() method, because else these arguments are shared across instances. As an example, try this:

    Code:
    var a = isc.RestDataSource.create();
    // a.operationBindings[0] = {operationType: 'fetch', dataProtocol: 'getParams'}
    
    a.operationBindings[0].requestProperties = {};
    // a.operationBindings[0] = {operationType: 'fetch', dataProtocol: 'getParams', requestProperties: {}}
    
    var b = isc.RestDataSource.create();
    // b.operationBindings[0] = {operationType: 'fetch', dataProtocol: 'getParams', requestProperties: {}} (thus, including the requestProperties object)
    This only happens if classes are defined like this:

    Code:
    isc.defineClass('RestDataSource').addProperties({ operationBindings: [ ... ] });
    It does not happen when instances are created like this:

    Code:
    isc.RestDataSource.create({ operationBindings: [ ... ] });
    I think this is invalid behavior. I am not sure how often this occurs within the framework, but I avoid these kinds of declaration.
    Last edited by wallytax; 5 Feb 2022, 22:16.

    #2
    You are correct that such settings are shared between all instances of the class. However, it's not invalid to do this, because it makes sense to do if the Array or Object is read-only, such as AutoChild properties / defaults.

    There are several places where the framework will actually warn you about doing this in an invalid place (such as listGrid.fields - use defaultFields instead). But we don't warn about it for every property because again it's a valid & more efficient approach if the data is read only.

    Comment

    Working...
    X