Skip to content

Conversation

@sbbowers
Copy link
Contributor

This change provides support for complex datatypes that require a more
complex serializer that JSON.stringify/parse

Decorator function signature now accepts named parameters via an object.
Maintains backward compatibility with original function signature as
well.

Example:

// Collection is just an array-like class I wrote
export class ModelStore extends Collection<BaseModel> {
    static serialize(collection: Collection<BaseModel>):string {
        return JSON.stringify(collection.slice());
    }

    static deserialize(serialized: string) {
        return new Collection<BaseModel>(JSON.parse(serialized).map((m:Object) => new BaseModel(m)));
    }
}

export class AppStore {
    @LocalStorage({serialize: ModelStore.serialize, deserialize: ModelStore.deserialize})
    private myData: ModelStore = new Collection<BaseModel>();
}

This change provides support for complex datatypes that require a more
complex serializer that JSON.stringify/parse

Decorator function signature now accepts named parameters via an object.
Maintains backward compatibility with original function signature as
well.
@skie
Copy link

skie commented Jul 9, 2016

Any reason this PR not merged yet? Looks super useful.

@sbbowers
Copy link
Contributor Author

sbbowers commented Jul 9, 2016

¯_(ツ)_/¯ In the interim, I'm doing something like the following. I keep all of my storage in a DataStore class that I inject into other classes that need it. DataStore is responsible for constructing real models on top of the generic objects provided by LocalStorage. For now, generic JSON.stringify is working for me for serialization. ModelStore has all the logic to do lookup/insert/delete, etc.

export type ConstructorFunc<T> = { new(...args : any[]): T; }

export class ModelStore<modelType extends BaseModel> {
    private data: Array<modelType> = [];

    constructor(data: Array<modelType>, modelType: ConstructorFunc<modelType>) {
        this.data = data;
        this.data.forEach((e, i, d) => { d[i] = new modelType(e) });
    }
}

export class DataStore {
    @LocalStorage() private myModels: Array<MyModel> = [];

    myModelStore: ModelStore<MyModel>;

    constructor() {
        this.myStore = new MyStore<MyModel>(this.myModels, MyModel);
    }
}```

@marcj
Copy link
Owner

marcj commented Jul 10, 2016

This library is almost not maintained anymore, pls see readme. However, I'm merging this for now as its really useful. Thanks!

@marcj marcj merged commit 370cd3b into marcj:master Jul 10, 2016
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

Successfully merging this pull request may close these issues.

3 participants