mobx icon indicating copy to clipboard operation
mobx copied to clipboard

binding stores

Open acopalipsis opened this issue 9 years ago • 4 comments

class Collection {
    data = [
        {id: "some_id", active: true, firstName: "", lastName: ""}
    ];
    
    getActive(){
        return this.data.find( item => item.active );
    }
}

class FirstStore {
    get firstName(){
        return this.collection.getActive().firstName;
    }
}
class LastStore {
    get firstName(){
        return this.collection.getActive().lastName;
    }
}

As the store learns that the collection is updated? It is possible to do with the help of the mobx?

acopalipsis avatar Mar 27 '17 12:03 acopalipsis

https://jsfiddle.net/gh/get/library/pure/mattruby/mobx-examples/tree/master/mobx-standalone/110-array Start playing with my array example here.

On Mon, Mar 27, 2017, 7:28 AM acopalipsis [email protected] wrote:

class Collection { data = [ {id: "some_id", active: true, firstName: "", lastName: ""} ];

getActive(){
    return this.data.find( item => item.active );
}

}

class FirstStore { get firstName(){ return this.collection.getActive().firstName; } } class LastStore { get firstName(){ return this.collection.getActive().lastName; } }

As the store learns that the collection is updated? It is possible to do with the help of the mobx?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx/issues/908, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIrcoY7ivjjcVBf8G2kOg8pO6H6D4YAks5rp6t9gaJpZM4MqMic .

mattruby avatar Mar 27 '17 12:03 mattruby

@mattruby You have in the example typical store. And I need when a collection is modified to update the stores.

acopalipsis avatar Mar 27 '17 13:03 acopalipsis

Use computeds on the derived stores that ref the other store

On Mon, Mar 27, 2017, 8:08 AM acopalipsis [email protected] wrote:

@mattruby https://github.com/mattruby You have in the example typical store. And I need when a collection is modified to update the stores.

— You are receiving this because you were mentioned.

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

mattruby avatar Mar 27 '17 13:03 mattruby

@mattruby

I think I figured out how to do. In this code, you have something to add-remove to maintain the style mobx?

@inject('store')
@observer
class Test extends Component<any, any> {
    public render(): ReactElement<any> {
        console.log(':::', this.props.store.name)
        return (
            <span>{this.props.store.name}</span>
        );
    }
}

class Collection {
    @observable
    public data: any[] = [
        {id: "first", name: "first", active: true}
    ];

    @computed
    get active(): any {
        return this.data.find( item => item.active );
    }

    @action
    public update(id, name): void {
        let item = this.data.find( item => item.id === id );
        let index = this.data.indexOf( item );

        item.name = name;

        this.data[ index ] = item;
    }
}



class Store {
    private collection: Collection;

    constructor(collection: Collection){
        this.collection = collection;
    }

    @computed
    public get name(): string {
        return this.collection.active.name;
    }
}

let collection: Collection = new Collection();
let store = new Store( collection );
let stores = { store };

window['setData'] = ( id, value ) => collection.update( id, value );

ReactDOM.render(
    <Provider {...stores}>
        <Test />
    </Provider>,
    document.querySelector( 'main' )
);

acopalipsis avatar Mar 27 '17 13:03 acopalipsis