sách gpt4 ai đã đi

ember.js - Ember 计算属性 vs Ember Observer

In lại 作者:行者123 更新时间:2023-12-05 00:55:06 28 4
mua khóa gpt4 Nike

我在这里看到的以前的问题似乎都没有涵盖何时使用 Ember 计算属性与 Ember Observer 的主题。我知道计算属性使用以前的属性来帮助生成新属性并在运行循环中更新。

Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,

fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
})
});

另一方面,观察者在运行循环之外更新,并且可以观察任何东西,甚至是计算属性。它会对任何类型的变化使用react。
Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,

fullName: Ember.computed('firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
}),

fullNameChanged: Ember.observer('fullName', function() {
// deal with the change
console.log(`fullName changed to: ${this.get('fullName')}`);
})
});

Ember 文档随后指出观察者通常被过度使用。有人可以举一个更好的例子来说明观察者的正确用法吗?他们还能看什么,不正确使用与正确使用的影响是什么?

源代码可以在 ember 文档中找到: https://guides.emberjs.com/v2.3.0/object-model/observers/

1 Câu trả lời

Computed Property uses previous attributes to help generate a new attribute and is updated in the run loop



是的,但是 懒洋洋地 --仅在被引用时才有意义, 它们的依赖项更改使缓存的值无效。

The Ember documentation then states that observers are typically over used.



是的,每当被监视的属性发生变化时,观察者都会同步触发,即使原因是重新计算不会使用的东西。对应该计算的属性使用观察者是经典的 Ember 反模式之一。

我检查了几个我工作的大型应用程序,发现观察者被用于诸如在发生变化时根据需要调用某些第三方库,或者在选择新的 UI 语言时更改应用程序的语言等事情。

关于ember.js - Ember 计算属性 vs Ember Observer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38828784/

28 4 0
Bài viết được đề xuất: python - Pandas 在最近的时间戳上合并数据帧
Bài viết được đề xuất: cmake - 在 custom_target 中执行 git describe
Bài viết được đề xuất: image - 图像列表 "modes"
Bài viết được đề xuất: php - PHP 何时在长时间运行的脚本中运行垃圾收集?
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com