Skip to content

nibynic/ember-data-fork

Repository files navigation

Ember Data Fork

Build Status Maintainability npm version

Track Ember Data model changes, accept or reject them with a simple fork interface.

Compatibility

  • Ember.js v3.24 or above
  • Ember CLI v3.24 or above
  • Node.js v12 or above

Installation

ember install ember-data-fork

Usage

Just call fork() on your model and then use the returned fork in the same way as you would use Ember Data models.

import { fork } from 'ember-data-fork';

let model = this.store.findRecord('person', 1);
let myFork = fork(model);

myFork.set('firstName', 'Lenny');
myFork.children.addObject(
  this.store.createRecord('person', {
    firstName: 'Oliver'
  })
);

myFork.isDirty; // true

// now you can:
myFork.rollback(); // reset to the initial state
myFork.apply(); // apply changes on the model
myFork.save(); // apply changes and save all changed models

Saving data

When you call save() on the fork, it will collect all modified models and save them in a sequence (starting from the deepest levels of nesting). If any of these saves fails, Ember Data Fork will try to revert models and fork to the state from before the save.

Deleting records

You can call deleteRecord() on a fork. This won't delete source model until you call save() on the fork.

import { fork } from 'ember-data-fork';

let model = this.store.findRecord('person', 1);
let myFork = fork(model);

myFork.deleteRecord();

myFork.isDeleted; // true
model.isDeleted; // false

myFork.save();

model.isDeleted; // true

To revert unsaved deletion just call rollbackDelete().

import { fork } from 'ember-data-fork';

let model = this.store.findRecord('person', 1);
let myFork = fork(model);

myFork.deleteRecord();

myFork.isDeleted; // true
model.isDeleted; // false

myFork.rollbackDelete();

myFork.isDeleted; // false
model.isDeleted; // false

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

About

Fork Ember Data records with ease

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •