Giter Club home page Giter Club logo

vue-data-branch's Introduction

Vue Data Branch

This is an experimental library to test a new way of managing global state in Vue applications.

How it works

The Branch Plugin structures the global state as one or multiple roots (think of individual state modules), where each root should have the entire state needed by a feature or a view.

Then, by adding the Branch Mixin to a component, we can pick any branch of the state to be mapped to the component's data.

When the component's branch data is mutated, it will update the root state.

The branch is an object that gets mapped to this.branch in the component's data. It is constructed by picking the key-value pairs of the object found at the specified object path.

All branches have the following properties:

  1. root - the name of the root to be used
  2. path - the string containing a valid object path
  3. keys - the array of keys to be picked and mapped to this.branch

Usage

main.js

import { BranchPlugin } from "vue-data-branch";

Vue.use(BranchPlugin, { roots: [
  {
    name: "someName",
    initialData: {
      some: {
        nested: {
          property: {
            a: 1,
            b: 2,
            c: 3,
            d: 4
          }
        }
      }
    }
  }
]});

SomeComponent.vue

import { BranchMixin } from "vue-data-branch";

export default {
  mixins: [BranchMixin],
  branch: {
    root: "someName",
    path: "some.nested.property",
    keys: ["a", "b"]
  },
  mounted() {
    console.log(this.branch); // will display { a: 1, b: 2 }
  },
  methods: {
    update() {
      this.branch.a += 1;  // branch properties can be mutated directly
    }
  }
}

If we want to dinamically construct the branch options, we can define the getRootName, getBranchKeys and getBranchPath methods on our component.

methods: {
  getRootName() {
    return "someName";
  },
  getBranchPath() {
    return "some.nested.property";
  },
  getBranchKeys() {
    return ["a", "b"];
  }
}

Validation

By default, when the branch is mutated, it will update the root's state automatically.
But, we can use the shouldUpdateBranch method in our component to control when to update the root state.

Example:

methods: {
  // the root state will be updated only if this.branch.a is even.
  shouldUpdateBranch() {
    if (this.branch.a % 2 === 0) {
      return true;
    }
    return false;
  }
}

Branch Syncing conflict

By default, all branches are kept in sync with the root data.

But, if we use the same branch in multiple components and we want to control how or when the component's branch data will be synced, then we can define the handleConflictSync method.

Example:

methods: {
  handleConflictSync() {
    if (this.syncedBranch.a % 2 !== 0) {
      return; // do not sync if the "a" property is not even
    }
    this.syncBranch(); // sync the branch manually
  }
}

Contributing

Feel free to dive in! Open an issue or submit PRs.

vue-data-branch's People

Contributors

devcatalin avatar

Stargazers

 avatar  avatar

Watchers

 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.