Code Monkey home page Code Monkey logo

branch-js's Introduction

Build Status

Branch.js

"branching" lets you make changes to an object without directly affecting the original object until you are ready to "merge" those changes back in. Similar to how you might "branch" a git repo, make some changes to it, then "merge" that branch back in. ex.:

var copy = branchjs(obj)
... Make a bunch of changes to copy here ...
copy.$commit().$merge(obj)

The most applicable use case for this is probably forms.

  • You can branchjs an object in order to 2-way bind to a form.
  • A successful form submission would trigger a $merge back to the original object
  • a $revert could be triggered by a "clear" button, in order to remove all unsaved changes.
  • You could "pre-load" some form changes to the branched object by making an update and $commiting them before backing the form. That way, the change is preserved and will be applied on $merge, but won't be blown away on a $revert (which we probably have bound the clear button)
  • Finally, in addition to just $mergeing back to your original object on a succesfull form submission ... since $merge ONLY applies the diff, you coud apply it to any number of objects that the changes might be applicable to.

Usage

var obj = {
  prop1: 'val1',
  prop2: ['val2']
}

branchjs

var branchedObj = branchjs(obj); //Create a 'branched' copy of the original object

$hasChanges

console.log(branchedObj.$hasChanges()) //false - no changes have been made to this object
branchedObj.prop1 = 'newval1';
console.log(branchedObj.$hasChanges()) //true - we've updated a property

$commit

branchedObj.$commit();
console.log(branchedObj.$hasChanges()) //false - changes are now "commited" to this branch

$revert

branchedObj.prop3 = 'val3';
branchedObj.$revert(); 
console.log(branchedObj.prop1) //newval1 - we've commited this change, so it is still there
console.log(branchedObj.prop3) //undefined - we reverted changes back to the last commit, so this is now `undefined` again

$merge

branchedObj.$merge(obj);
console.log(obj.prop1) //newval1 - we merged any commited changes back into our original object

branchedObj = branchjs(obj); //start over
branchedObj.prop2.push('val3');
branchedObj.prop2.push('val4');
branchedObj.$commit();

var anotherobject = {
  prop2: ['original']
}

var yetanotherobject = {
  prop2: ['test', 'values']
}

branchedObj
  .$merge(anotherobject)
  .$merge(yetanotherobject)

//This is important ... it ONLY applies the diff to the target objects
console.log(anotherobject.prop2); // ['original', 'val3', 'val4']
console.log(yetanotherobject.prop2); // ['test', 'values', 'val3', 'val4']

branch-js's People

Contributors

jasonstoltz avatar

Stargazers

Tsz Lam avatar Nikolaos Dionelis avatar Johnny Sheeley avatar Fernando Moraes avatar Mike Davis avatar Court Ewing avatar

Watchers

James Cloos avatar Nikolaos Dionelis avatar  avatar

Forkers

foxandxss

branch-js's Issues

Looks great

This library looks great by providing the ability in deferring update to any object. But looks like it is so quiet here? Is it because there is an alternative library?

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.