Git Product home page Git Product logo

vue-depot's Introduction

vue-depot

Simple state manager based and for Vue with support for vue-devtools.

Installation

npm install vue-depot

Usage

Creating state manager

//store.ts
import {Depot, Store, module} from 'vue-depot';
@Store(true)
class RootStore extends Depot {
    public lastUpdate: Date = new Date();
    public todos: string[] = [];
    get todosCount(): number {
        return this.todos.length;
    }
    public addTodo(text: string) {
        this.todos.push(text);
    }
    public async asyncAddTodo(test: string) {
        return Promise((r) => {
            setTimeout(() => {
                this.addTodo(test);
                r();
            }, 1000)
        });
    }
}
export const BaseStore = new RootStore();

Usage in other file

import {BaseStore} from './store.ts'
new Vue({
    data() {
        return {
            store: BaseStore
        };
    },
    computed: {
        todosList() {
            return this.store.todos.join();
            // or return BaseStore.todos.join();
        }
    },
    methods: {
        add() {
            this.store.addTodo('newTodo');
            // or BaseStore.addTodo('newTodo');
            // or @click="store.addTodo('newTodo')" inside template
        }
    }
})

@Store(isRootInstance?: boolean)

  • isRootInstance - needed only for vue-devtools to recognise root instance and emit "init" event.

Convert given class to Vue component so new instance is in fact new Vue component with full support of features like smart getters, reactive props and methods like $watch. Decorator also inject state getter/setter required for vue-devtools (extending with Depot is optional and only add typings for .state)

Modules

It is possible to nesting instances in this way:

@Store()
class User { ...some props and actions }
@Store(true)
class RootStore extends Depot {
    @module(User)
    public user: User = new User();
    @module(User)
    public userList: User[] = [];
}
export const BaseStore = new RootStore();

IMPORTANT!!! Remember to manipulate with Arrays like in Vue: use Vue.set(BaseStore.userList, 0, value) instead of BaseStore.userList[0] = value;

@module(moduleClass: T)

  • moduleClass - necessary for casting raw object into module. Define nested module

Depot

Depot.state

Setter/getter to get or set raw-data model. Mainly used by vue-devtools and may be expensive. It support module instances casting so raw object will be converted to module object (but only for modules).

vue-depot's People

Contributors

wszerad avatar

Stargazers

Viktor Zhokhov avatar

Watchers

James Cloos avatar Viktor Zhokhov avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.