Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize addon #326

Draft
wants to merge 29 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2013e75
Bump Ember to 3.28, upgrade packages, modernize addon
charlesfries Sep 8, 2021
9b80c6b
Lint
charlesfries Sep 8, 2021
23d2c9b
Modernize
charlesfries Sep 8, 2021
46630ad
Add eslint rules
charlesfries Sep 8, 2021
8c99da0
Cleanup
charlesfries Sep 8, 2021
7ffcc1b
Revert get removal
charlesfries Sep 8, 2021
b3f98eb
Uninstall chalk
charlesfries Sep 13, 2021
43f02e3
More native classes, update README
charlesfries Sep 14, 2021
5a72464
Revert "Uninstall chalk"
charlesfries Sep 14, 2021
46a2e17
Diff changes
charlesfries Sep 14, 2021
5e3783a
Revert automatic formatting for review (tmp)
charlesfries Sep 14, 2021
9e24b87
Revert
charlesfries Sep 14, 2021
19cb5bd
Revert
charlesfries Sep 14, 2021
89de7e1
Revert
charlesfries Sep 14, 2021
c70700b
Uninstall ember-cli-htmlbars, function name fix, more reverts
charlesfries Sep 14, 2021
a9ca71b
Revert
charlesfries Sep 14, 2021
848cb27
Cleanup
charlesfries Sep 14, 2021
c835ab7
Revert
charlesfries Sep 14, 2021
0cfc670
Restore ember-cli-htmlbars
charlesfries Sep 14, 2021
6e9dae1
Revert
charlesfries Sep 14, 2021
da9c9e7
Revert
charlesfries Sep 14, 2021
9fc9b86
Uninstall ember-one-way-controls, bump ember-copy
charlesfries Sep 14, 2021
81effaa
Bump packages, uninstall ember-cli-template-lint, disable Bower
charlesfries Sep 14, 2021
f39bf3d
Bump more packages, re-enable jQuery
charlesfries Sep 14, 2021
b318714
Merge branch 'master' of https://github.com/funkensturm/ember-local-s…
charlesfries Oct 9, 2021
000f27a
Merge branch 'funkensturm-master'
charlesfries Oct 9, 2021
6c9cccf
Update yarn.lock
charlesfries Oct 9, 2021
6e14cf2
Uninstall ember-cli-inuitcss, fix decorator issue
charlesfries Oct 9, 2021
ef46ab5
Fix tests
charlesfries Oct 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .bowerrc

This file was deleted.

2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = {
'ember/no-classic-classes': 'off',
'ember/no-classic-components': 'off',
'ember/no-component-lifecycle-hooks': 'off',
'ember/no-computed-properties-in-native-classes': 'off',
'ember/no-get': 'off',
'ember/no-jquery': 'off',
'ember/no-mixins': 'off',
Expand Down Expand Up @@ -63,6 +64,7 @@ module.exports = {
files: ['tests/**/*-test.{js,ts}'],
extends: ['plugin:qunit/recommended'],
rules: {
'ember/no-get': 'off',
'qunit/no-assert-equal-boolean': 'off',
'qunit/no-conditional-assertions': 'off',
'qunit/no-ok-equality': 'off',
Expand Down
171 changes: 83 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ The documentation is for versions `>= 1.0.0` if you are looking for older versio

If you upgrade from a version `<= 0.1.5` you need to set a `legacyKey` on the computed `storageFor`:
```javascript
export default Ember.Component.extend({
settings: storageFor('settings', { legacyKey: 'your-old-key' })
});
export default class MyComponent extends Component {
@storageFor('settings', { legacyKey: 'your-old-key' }) settings;
}
```

See the [CHANGELOG](CHANGELOG.md)
Expand Down Expand Up @@ -57,8 +57,9 @@ To activate it there are the following options:

```js
// config/environment.js
module.exports = function() {
var ENV = {

module.exports = function (environment) {
let ENV = {
modulePrefix: 'my-app',
'ember-local-storage': {
namespace: true, // will use the modulePrefix e.g. 'my-app'
Expand All @@ -75,8 +76,9 @@ This addon autodetects if you use ember-data and will include the support for em

```js
// config/environment.js
module.exports = function() {
var ENV = {

module.exports = function (environment) {
let ENV = {
modulePrefix: 'my-app',
'ember-local-storage': {
includeEmberDataSupport: false
Expand Down Expand Up @@ -126,30 +128,32 @@ export default Storage;

```javascript
// app/controllers/application.js
import Ember from 'ember';

import Controller from '@ember/controller';
import { storageFor } from 'ember-local-storage';
import { action } from '@ember/object';

export default Ember.Controller.extend({
stats: storageFor('stats'),

actions: {
countUp() {
this.incrementProperty('stats.counter');
},
resetCounter() {
this.get('stats').clear();
// or
// this.get('stats').reset();
// this.set('stats.counter', 0);
}
export default class ApplicationController extends Controller {
@storageFor('stats') stats;

@action countUp() {
this.incrementProperty('stats.counter');
}
});

@action resetCounter() {
this.get('stats').clear();
// or
// this.get('stats').reset();
// this.set('stats.counter', 0);
}
}
```

```handlebars
{{! app/templates/application.hbs}}
<button {{action "countUp"}}>Page Visits: {{stats.counter}}</button>
<button {{action "resetCounter"}}>X</button>
{{! app/templates/application.hbs }}

<button {{on "click" this.countUp}}>Page Visits: {{this.stats.counter}}</button>
<button {{on "click" this.resetCounter}}>X</button>
```

#### Array
Expand All @@ -166,6 +170,7 @@ ember g storage anonymous-likes -a -s

```javascript
// app/storages/anonymous-likes.js

import StorageArray from 'ember-local-storage/local/array';

const Storage = StorageArray.extend();
Expand All @@ -182,28 +187,29 @@ export default Storage;

```javascript
// app/components/like-item.js
import Ember from 'ember';

import Component from '@glimmer/component';
import { storageFor } from 'ember-local-storage';
import { action } from '@ember/object';

export default Ember.Component.extend({
anonymousLikes: storageFor('anonymous-likes'),
export default class LikeItemComponent extends Component {
@storageFor('anonymous-likes') anonymousLikes;

isLiked: computed('id', function() {
get isLiked() {
return this.get('anonymousLikes').includes(this.get('id'));
}),
}

actions: {
like: function(id) {
this.get('anonymousLikes').addObject(id);
}
@action like(id) {
this.get('anonymousLikes').addObject(id);
}
});
}
```

```handlebars
{{! app/templates/components/like-item.hbs}}
{{#unless isLiked}}
<button {{action "like" id}}>Like it</button>
{{! app/components/like-item.hbs}}

{{#unless this.isLiked}}
<button type="button" {{on "click" (fn this.like id)}}>Like it</button>
{{else}}
You like it!
{{/unless}}
Expand Down Expand Up @@ -268,12 +274,13 @@ If you use namespaced models e.g. `blog/post` you have to add the `modelNamespac

```js
// app/adapters/blog/post.js

import Adapter from 'ember-local-storage/adapters/local';
// or import Adapter from 'ember-local-storage/adapters/session';

export default Adapter.extend({
modelNamespace: 'blog'
});
export default class BlogPostAdapter extends Adapter {
modelNamespace = 'blog';
}
```

#### Model
Expand All @@ -282,34 +289,26 @@ Your model is a `DS.Model` with two new relationship options

```javascript
// app/models/post.js
import DS from 'ember-data';

const {
Model,
attr,
hasMany
} = DS;
import Model, { attr, hasMany } from '@ember-data/model';

export default Model.extend({
name: attr('string'),
export default class PostModel extends Model {
@attr('string') name;

comments: hasMany('comment', { async: true, dependent: 'destroy' })
});
@hasMany('comment', { async: true, dependent: 'destroy' }) comments;
}
```

```javascript
// app/models/comment.js
import DS from 'ember-data';

const {
Model,
attr,
belongsTo
} = DS;
import Model, { attr, belongsTo } from '@ember-data/model';

export default Model.extend({
name: attr('string'),
export default class CommentModel extends Model {
@attr('string') name;

post: belongsTo('post', { async: true, autoSave: true })
});
@belongsTo('post', { async: true, autoSave: true }) post;
}
```

**Options**
Expand Down Expand Up @@ -376,8 +375,9 @@ You have to add `fileExport` option to the `environment.js`:

```javascript
// config/environment.js
module.exports = function() {
var ENV = {

module.exports = function (environment) {
let ENV = {
'ember-local-storage': {
fileExport: true
}
Expand All @@ -388,44 +388,39 @@ module.exports = function() {
The initializer provides `exportData()` and `importData()` on the store. Both return a Promise.

```javascript
import Ember from 'ember';

const {
Route
} = Ember;
import Route from '@ember/routing/route';
import { action } from '@ember/object';

export default Route.extend({
readFile: function(file) {
export default class IndexRoute extends Route {
readFile(file) {
const reader = new FileReader();

return new Ember.RSVP.Promise((resolve) => {
reader.onload = function(event) {
return new Promise((resolve) => {
reader.onload = function (event) {
resolve({
file: file.name,
type: file.type,
data: event.target.result,
size: file.size
size: file.size,
});
};

reader.readAsText(file);
});
},
actions: {
importData: function(event) {
this.readFile(event.target.files[0])
.then((file) => {
this.store.importData(file.data);
});
},
exportData: function() {
this.store.exportData(
['posts', 'comments'],
{download: true, filename: 'my-data.json'}
);
}
}
});

@action async importData(event) {
const file = await this.readFile(event.target.files[0])
this.store.importData(file.data);
}

@action exportData() {
this.store.exportData(
['posts', 'comments'],
{download: true, filename: 'my-data.json'}
);
}
}
```

**importData(content, options)**
Expand Down
Loading