mobx icon indicating copy to clipboard operation
mobx copied to clipboard

Observable extended array

Open Kukkimonsuta opened this issue 10 years ago • 7 comments

I've got follwing class:

class CollectionPage<T> extends Array<T> {
    constructor(data: Array<T>, page: number, pageCount: number) {
        super();

        this.page = page;
        this.pageCount = pageCount;

        this.push.apply(this, data);
    }

    page: number;
    pageCount: number;
}

Is there a way to transform instance of this class into an observable array while keeping both extra properties?

Kukkimonsuta avatar Aug 02 '16 15:08 Kukkimonsuta

To achieve this behaviour you have to extend mobx internal class ObservableArray, but you can't. Instead I would use something like

class CollectionPage<T> {
    @observable data: IObservableArray<T>
    @observable page: number
    @observable pageCount: number

    constructor(data: T[], page: number, pageCount: number) {
        this.data = observable(data);
        this.page = page;
        this.pageCount = pageCount;
    }
}

andykog avatar Aug 04 '16 07:08 andykog

Allright, I suppose there are no plans to add support for extending ObservableArray? If not, please close this.

Kukkimonsuta avatar Aug 06 '16 15:08 Kukkimonsuta

@mweststrate

andykog avatar Aug 06 '16 15:08 andykog

Nope, it is not a much requested feature and might make other generic functionality harder in general.

mweststrate avatar Aug 06 '16 15:08 mweststrate

No, observable array will actually be gone in the future in favor of real arrays with proxies. So that brings you back to what you should do: extend the Array class.

....

....

Which is not possible in javascript. (Well, it is limited)

On di 2 jan. 2018 17:05 Francesco [email protected] wrote:

No hope?

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx/issues/456#issuecomment-354800775, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvGhMGNWJT4dXr0IlNbrLfmzw-gMAXPks5tGlO9gaJpZM4JathS .

mweststrate avatar Jan 02 '18 16:01 mweststrate

Thanks for the answer, I got the point. It would be amazing to create your own iterable & observable class but javascript has limitation in that sense :/

cescox avatar Jan 02 '18 17:01 cescox

Actually doesn't that mean that you can create own observable iterable once proxy overlords come to us?

class CollectionPage extends Array {
    constructor(data, page, pageCount) {
        super();

        this.page = page;
        this.pageCount = pageCount;

        this.push.apply(this, data);
    }
}

var inst = new CollectionPage(["cookies", "foos", "bars"], 0, 5);
var prox = new Proxy(inst, { 
    set: function (obj, prop, value) { 
        console.log('change', obj, prop, value); obj[prop] = value;
        return true;
    } 
});

console.log(inst);

prox[1] = "milk";
prox.page = 3;
prox.pop();
prox.push("more milk");

console.log(inst);

This works fine in chrome console right now, so I'd expect mobx to do it's magic and react to changes on both array values and page, pageCount properties.

Kukkimonsuta avatar Jan 02 '18 17:01 Kukkimonsuta