Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/onechiporenko/ember-model-original-attributes
https://github.com/onechiporenko/ember-model-original-attributes
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/onechiporenko/ember-model-original-attributes
- Owner: onechiporenko
- License: mit
- Created: 2018-01-04T20:56:28.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-23T08:56:27.000Z (8 months ago)
- Last Synced: 2024-04-13T21:02:00.016Z (7 months ago)
- Language: JavaScript
- Size: 191 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ember-model-original-attributes
[![Build Status](https://travis-ci.org/onechiporenko/ember-model-original-attributes.svg?branch=master)](https://travis-ci.org/onechiporenko/ember-model-original-attributes)
[![npm version](https://badge.fury.io/js/ember-model-original-attributes.png)](http://badge.fury.io/js/ember-model-original-attributes)> Add new computed properties to Models with values equal to some saved attribute's values.
## Install
```bash
ember install ember-model-original-attributes
```## Usage
Create a section `originalAttributes` in the `config/environment.js`:
```javascript
module.exports = function(/* environment, appConfig */) {
return {
originalAttributes: {
prefix: 'old',
models: {
user: {
attrs: ['firstName', 'lastName']
}
}
}
};
};
```Model `user` will have two new computed properties called `oldFirstName` and `oldLastName`. Both of them will be equal to the saved values of the `firstName` and `lastName`.
Existing computed properties, attributes or relationships won't be overridden.
Prefix `original` is used by default.
Property `models` is a hash with keys equal to Model names. Its values are hashes with property `attrs`. Each `attrs` is an array with attribute names that should be processed.
```javascript
store.findRecord('user', '1').then(user => {
user.get('firstName'); // 'Jim'
user.get('oldFirstName'); // 'Jim'
user.set('firstName', 'Tychus');
user.get('firstName'); // 'Tychus'
user.get('oldFirstName'); // 'Jim'
user.save().then(() => {
user.get('firstName'); // 'Tychus'
user.get('oldFirstName'); // 'Tychus'
});
});
```