Ember Octane中出现了许多新功能,但是对我来说,没有什么比自动跟踪(自动跟踪)更令人兴奋了。自动跟踪是Ember的新反应系统,它使Ember可以知道状态值(例如标记为的属性@tracked
)何时已更改。这是一个重大的更新,包括在新内核之上完全重写了一些最古老的Ember抽象。
译者:Chris Garrett-在LinkedIn上工作,是Ember js框架的核心贡献者之一。他积极参与了该框架的新版本-Ember Octane的开发。尽管他的系列是为Ember开发人员编写的,但它涉及了对所有Web程序员都有用的概念。
乍一看,自动跟踪与反应性模型非常相似Ember Classic
(基于计算出的属性,观察者和Ember.set()
)。如果将它们并排比较,则主要区别在于注释的放置位置。在Octane中,您注释依赖的属性,而在Classic中,您注释getter和setter。
class OctaneGreeter {
@tracked name = 'Liz';
get greeting() {
return `Hi, ${this.name}!`;
}
}
class ClassicGreeter {
name = 'Liz';
@computed('name')
get greeting() {
return `Hi, ${this.name}!`;
}
}
但是,如果深入研究,您会发现自动跟踪更加灵活和强大。例如,您可以自动跟踪功能的结果,这在Ember Classic中是不可能的。以一个简单的2D视频游戏的模型为例:
class Tile {
@tracked type;
constructor(type) {
this.type = type;
}
}
class Character {
@tracked x = 0;
@tracked y = 0;
}
class WorldMap {
@tracked character = new Character();
tiles = [
[new Tile('hills'), new Tile('beach'), new Tile('ocean')],
[new Tile('hills'), new Tile('beach'), new Tile('ocean')]
[new Tile('beach'), new Tile('ocean'), new Tile('reef')],
];
getType(x, y) {
return this.tiles[x][y].type;
}
get isSwimming() {
let { x, y } = this.character;
return this.getType(x, y) === 'ocean';
}
}
, isSwimming
, character
( x / y). , , getType
, . CP (. .: Computed Properties — Ember Classics), - . .
, Ember Classic. ! , , Ember , Octane.
-, , . , . , . , , , , .
7 . , « », , :
- ? ←
- ?
- — TrackedMap
- — @localCopy
- — RemoteData
- — effect()
, , Octane. -, , , ! Discord.
: «»?
, « », , . . , .
: .
«» , « » « ». , « Ember» « Vue», , . , .
, ! .

( (streams))
, , , , : «» (declarative) «» (state). « » , «» «» ? "" (state)? , , .
(state)
«» «». , , ; , "". JavaScript :
, (root state), , . , , , . :
let a = 1;
let b = 2;
function aPlusB() {
return a + b;
}
aPlusB()
, a
b
. , . , , a
b
, aPlusB()
. , a
b
, aPlusB()
, .
, «», «» «», , , . , :
, , , .
, - .
«» , (). , — « »? , , , , , , , ( , ).
aPlusB
. , , :
- ( ).
a + b
.
let a = 1;
let b = 2;
let aPlusB = a + b;
, , . , a
b
. aPlusB
, :
let a = 1;
let b = 2;
let aPlusB = a + b;
a = 4;
aPlusB = a + b;
b = 7;
aPlusB = a + b;
, . , a
, aPlusB
. API , , :
export default class Counter extends Component {
count = 0;
@action
incrementCount() {
this.count++;
this.rerender();
}
}
, rerender()
, , . , , - , , .
, «» . aPlusB()
.
let a = 1;
let b = 2;
function aPlusB() {
return a + b;
}
, , , . , «» , . , aPlusB()
, . , , , .
, , Ember, Vue Svelte, (, ) JSX , , JSX . HTML — , , , . HTML, , . «» , , , DOM, , DOM , .
FP (Functional Programming)?
. «» «» «». , . , HTML, , - , .
( FP), . «» — .
let a = 1;
let b = 2;
function plus(first, second) {
return first + second;
}
plus(a, b);
. , , .

, ( ). , FP , , , . , , , .
, . , , , .
, , . , «» FP, - (, , . « », !). , .
, :
- «» « ». , , , .
- «» , . ( , ):
- « » , , , . HTML , — , . , , .
. , , , - , - . , , «» .
: , . , , )