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

[DOC] Added examples for arrayContent[Will|Did]Change #18306

Merged
merged 2 commits into from
Sep 1, 2019
Merged
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions packages/@ember/-internals/runtime/lib/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,54 @@ const ArrayMixin = Mixin.create(Enumerable, {
invalidate any related properties. Pass the starting index of the change
as well as a delta of the amounts to change.

```app/components/show-post.js
import Component from '@ember/component';
import EmberObject from '@ember/object';

const Post = EmberObject.extend({
body: '',
save() {}
})

export default Component.extend({
attemptsToModify: 0,
successfulModifications: 0,
posts: null,

init() {
this._super(...arguments);
mukilane marked this conversation as resolved.
Show resolved Hide resolved

this.posts = [1, 2, 3].map(i => Post.create({ body: i }));
this.posts.addArrayObserver(this, {
willChange() {
this.incrementProperty('attemptsToModify');
},
didChange() {
this.incrementProperty('successfulModifications');
}
});
},

actions: {
editPost(post, newContent) {
let oldContent = post.body,
postIndex = this.posts.indexOf(post);

this.posts.arrayContentWillChange(postIndex, 0, 0); // attemptsToModify = 1
post.set('body', newContent);

post.save()
.then(response => {
this.posts.arrayContentDidChange(postIndex, 0, 0); // successfulModifications = 1
})
.catch(error => {
post.set('body', oldContent);
})
}
}
});
```

@method arrayContentWillChange
@param {Number} startIdx The starting index in the array that will change.
@param {Number} removeAmt The number of items that will be removed. If you
Expand All @@ -510,6 +558,15 @@ const ArrayMixin = Mixin.create(Enumerable, {
invalidate any related properties. Pass the starting index of the change
as well as a delta of the amounts to change.

```javascript
let arr = [1, 2, 3, 4, 5];

arr.copyWithin(-2); // [1, 2, 3, 1, 2]
// arr.lastObject = 5
arr.arrayContentDidChange(3, 2, 2);
// arr.lastObject = 2
```

@method arrayContentDidChange
@param {Number} startIdx The starting index in the array that did change.
@param {Number} removeAmt The number of items that were removed. If you
Expand Down