On Github ryanlabouve / reacting-to-change-emberjs
Created by Ryan LaBouve / @ryanlabouve
Charles Lowell, Reactive Modeling with Ember
Two Static Properties (A,B) and a Visualized Dyamic Relationship
Adobe Color CC
Charles Lowell, Reactive Modeling with Ember
What we're looking for
For the reactive part
Lazy Evaluation ( So we are not crunching data before we need it ) Composeable ( Keep relationships can reusable and lego-ish ) Testable ( Clean encapsulated pieces we can test) Easy to Reason AboutPrimitive #1
// Setter and Getter Required for Observation
const human = Ember.Object.create({});
human.set('name', 'bob');
human.get('name'); // => "name"
// app/application/route
import Ember from 'ember';
const Human = Ember.Object.extend();
export default Ember.Route.extend({
model() {
return Human.create({ firstName: 'bob' });
}
});
{{! app/templates/application.hbs }}
Why, hello there {{model.firstName}}!<br>
<p>
First Name:
{{input value=model.firstName}}
</p>
Primitive #2
import Ember from 'ember';
const { computed } = Ember;
const Human = Ember.Object.extend({
fullName: computed( 'firstName', 'lastName', function() {
return `${this.get('firstName')} ${this.get('lastName')}`;
})
});
export default Ember.Route.extend({
model() {
return Human.create({ firstName: 'bob', lastName: 'smith' });
}
});
Why, hello there {{model.fullName}}!<br>
<p>
First Name:
{{input value=model.firstName}}
</p>
<p>
<label>Last Name:</label>
{{input value=model.lastName}}
</p>
Yey lazy evaluation! Mike North, Compose All the Things
Forming more complex reactive relationships
import Ember from 'ember';
import EmberCPM from 'ember-cpm';
const { Macros: { sum, difference, product }} = EmberCPM;
export default Ember.Component.extend({
num1: 45,
num2: 3.5,
num3: 13.4,
num4: -2,
total: sum(
sum('num1', 'num2', 'num3'),
difference('num3', 'num2'),
product(difference('num2', 'num1'), 'num4')
)
});
With great power...
I'm tired of figuring out how much to eat.
How much to eat based on amount of activity
https://github.com/ryanlabouve/how-much-to-eat-example
ember new how-much-to-eat ember install ember-cli-sass ember install ember-paper npm install ember-cpm --save-dev ember generate route application ember generate controller application ember serve