I am always curious about how do those libraries/frameworks like React
, Angular
, Ember
work in background. I know it all about design patterns, which appeals me very much.
Now, I have been working on React for about 2 years, and AngularJS for some time. It’s time to start an adventure in Ember world. Items below are my thoughts while I went through the official documentation, and will be kept updating.
Ember framework/ecosystem behaves like a custodian, it
- takes
template
,component
,service
,data
,route
, etc defined by developers, via interfaces. - ditches them together in background
While React is more like a library, it works together with other library like Redux, React Router, Webpack to build up applications.
- takes
Ember CLI
can deal with file system, create scaffolds based on convention over configurationthis
inhbs
:- Each template / component is an object / instance
- what does
this
refer to? / what is the context? - why not using function programming, getting rid of
this
/ context?
.hbs
: will be pre-compiled before it converted into valid html?- Compilation workflow?
- only handler-bar syntax will be scanned
- other valid html syntax will be omitted
- valid html generated
- Compilation workflow?
Ember’s rendering engine
almost all functions are injected into
Ember
, if they are intended to used in another file.- eg.: helper functions will be in handlebar file, are injected by
helper()
- eg.: helper functions will be in handlebar file, are injected by
function invokation in
hbs
:function-name arg1 arg2
, will be executed asfunction-name([arg1, arg2])
Router => Route [modal + template] => template with its model (consist of resuable components)
Ember Data
is data management lib, in charge of API call and other data massages.extend()
: use Ember Component class/interface; Meanwhile, add customed function / data1
2
3
4Component.extend({
isBig: false,
productList: []
})A component consists of two parts:
- UI part:
.hbs
file (app/templates/components/list-filter.hbs
) - Logic Part:
component
object (app/components/list-filter.js
)
- UI part:
A template can also have its own logic part:
controller
object- UI part:
.hbs
file (app/templates/rentals.hbs
) - Logic Part: controller object (
app/controllers/rentals.js
)
- UI part:
Ember.Object
expand properties and methods on original JS object
make it
observable
(design pattern), used heavily in Ember env systemIssue:
create()
to instantiate a object with preset properties, usinginit()
at the same timeProblem: preset properties and init properties which one called first?
- properties in
init
beats instance preset properties - the hash passed to the create method is essentially treated as a mixin that is applied before init is run
- My early exploration encounterring such issue: seem like this is a good sign, which give me an impression this framework is not with a simple philosophy. But hold on, let me dig on.
- properties in
How to overwrite
init
inreopen
?1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36const Person = Ember.Object.extend({
init() {
this.set("shoppingList", Ember.A(['eggs', 'cheese']));
},
addItem(item) {
this.shoppingList.pushObject(item);
}
});
Person.reopen({
init() {
this._super();
this.set("age", 10);
}
})
let p1 = Person.create({
name: 'Stefan Penner',
});
let p2 = Person.create({
name: 'Robert Jackson',
});
let p3 = Person.create({
name: "a",
age: 199,
kkk: function(){console.log("333")}
})
console.log(p1.age);
console.log(p2.age);
console.log(p3.age);
p3.set('age', 3333)
console.log(p3.age);