Skip to content
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

Observable extended array #456

Closed
Kukkimonsuta opened this issue Aug 2, 2016 · 7 comments
Closed

Observable extended array #456

Kukkimonsuta opened this issue Aug 2, 2016 · 7 comments

Comments

@Kukkimonsuta
Copy link

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?

@andykog
Copy link
Member

andykog commented Aug 4, 2016

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;
    }
}

@Kukkimonsuta
Copy link
Author

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

@andykog
Copy link
Member

andykog commented Aug 6, 2016

@mweststrate

@mweststrate
Copy link
Member

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

@mweststrate
Copy link
Member

mweststrate commented Jan 2, 2018 via email

@francesco-carrella
Copy link

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 :/

@Kukkimonsuta
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants