Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Use ViewModels instead of doing data formatting inside templates #1982

Closed
faustbrian opened this issue May 28, 2020 · 1 comment
Closed

Use ViewModels instead of doing data formatting inside templates #1982

faustbrian opened this issue May 28, 2020 · 1 comment
Labels
Type: Refactor The pull request improves or enhances an existing implementation.
Milestone

Comments

@faustbrian
Copy link
Contributor

faustbrian commented May 28, 2020

View models are entities that take care of preparing a data model for usage in templates. This ensures that the data model itself is not leaked into the view and allows to abstract away all kinds of data manipulation and formatting.

class TransactionViewModel {
    readonly #data;

    public constructor (data: object) {
        this.#data = data;
    }

    get id () {
        return truncate(this.#data.id);
    }

    get sender () {
        return knownWallets[this.#data.sender];
    }

    get recipient () {
        return knownWallets[this.#data.recipient];
    }

    get amount () {
        return this.#data.amount.toFixed(2);
    }

    get fee () {
        return this.#data.fee.toFixed(2);
    }

    get memo () {
        return Censor(this.#data.memo);
    }
}

These view models could then be instantiated through a factory as shown below.

class ViewModelFactory {
    public static make(type: string, data: object) {
        return new {
            transaction: TransactionViewModel,
        }[type](data)
    }
}

To make the usage within the wallet more seamless it could be registered as a binding on the global vue instance.

import { ViewModelFactory } from "@arkecosystem/platform-sdk";

export default {
	install(Vue) {
		Vue.prototype.$viewModel = new ViewModelFactory();
	},
};

These might be implemented by the Platform SDK and the wallets will be forced to use them.

@faustbrian faustbrian added the Type: Refactor The pull request improves or enhances an existing implementation. label May 28, 2020
@faustbrian faustbrian added this to the 3.0.0 milestone May 28, 2020
@faustbrian
Copy link
Contributor Author

This will be handled by the SDK to ensure both wallets use the same data formatting and representation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Refactor The pull request improves or enhances an existing implementation.
Projects
None yet
Development

No branches or pull requests

1 participant