NOT server side MVC
Many of the same terms
but they mean different things
Ember | Rails | My Reaction |
---|---|---|
Router | config/routes.rb | makes sense |
Route | Controller action | wat |
Controller | Service | wat |
Model | Model | Yay! |
View | Presenter/Decorator | wat |
Template | View/template | wat |
Component | Function |
---|---|
Model | Object wrapper for business data |
View | Interaction logic |
Controller | Business logic |
Route | Controller setup / Model lookup & filtering |
Template | Markup for UI * |
Router | Browser/URL logic * |
* These components are artifacts of developing an app
to be run inside of a browser.
UselessApp = Ember.Application.create();
<script type="text/x-handlebars">
<h1>This app is useless!</h1>
</script>
UselessApp = Ember.Application.create();
UselessApp.Router.map(function(){
this.route('index',{ path:'' })
});
UselsssApp.IndexRoute = Ember.Route.extend({});
UselsssApp.IndexController = Ember.ObjectController.extend({});
UselsssApp.IndexView = Ember.View.extend({});
<script type="text/x-handlebars" id="index">
<h1>This app is verbose AND useless!</h1>
</script>
UselessApp.Person = Ember.Object.extend({
firstName: null,
lastName: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName')
});
var me = UselessApp.Person.create({
firstName:'Jeremy',
lastName: 'Green'
});
alert( "Hi, I'm " + me.get('fullName') );
UselessApp.IndexRoute = Ember.Route.extend({
model: function(params){
return UselessApp.Person.create({
firstName:"Jeremy",
lastName:"Green"
});
}
});
<script type="text/x-handlebars">
<h1>This app is useless!</h1>
<hr />
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="index">
<p>Created by : {{fullName}}</p>
</script>
They can't contain much logic.
Logic goes in the View.
<script type="text/x-handlebars" data-template-name="index">
<p>Created by : {{fullName}}</p>
<p>
{{view Em.TextField valueBinding='content.firstName'}}
{{view Em.TextField valueBinding='content.lastName'}}
</p>
</script>
Manipulaiton of models and triggering of related events.
Also a good place for transaction management.
UselessApp.IndexController = Ember.ObjectController.extend({
isEditing: false,
toggleEditing: function(){this.set('isEditing'),!this.set('isEditing')}
});
<script type="text/x-handlebars" data-template-name="index">
<p>Created by : {{fullName}} - <a href="#"{{action toggleEditing}}>Edit</a></p>
{{#if isEditing}}
<p>
{{view Em.TextField valueBinding='content.firstName'}} {{view Em.TextField valueBinding='content.lastName'}}
</p>
{{/if}}
</script>
MyApMyApp.SelectableModelListController = Ember.ArrayController.extend({
selectNone:function(){
this.get('content').forEach(function(selectableModel){
selectableModel.set('selected',false);
});
},
selectAll:function(){
this.get('content').forEach(function(selectableModel){
selectableModel.set('selected',true);
});
}
});
The 'map' for distinct parts of your app
gives you bookmarkable/shareable URLs within your app
handles reading/writing the URL bar
passes 'path variables' into your Route
UselessApp.Router.map(function() {
this.resource('random_echo',{path:'random_echo/:echo_text'});
});
<script type="text/x-handlebars" data-template-name="random_echo">
<h3>Random : {{random}}</h3>
<h3>Echo : {{echo_text}}</h3>
</script>
MyApp.Router.map(function() {
this.resource('about');
this.resource('galleries',function(){
this.resource('gallery',{ path: ':gallery_id' }, function(){
this.route('edit', {path: 'edit'})
})
this.route('new');
})
});
http://myapp.com/#/about
http://myapp.com/#/galleries
http://myapp.com/#/galleries/123
http://myapp.com/#/galleries/123/edit
http://myapp.com/#/galleries/new
This is the place for jQuery DOM selectors and
integration with other JS libs.
Probably.
From the Ember View doc:
Views in Ember.js are typically only created for the following reasons:
When you need sophisticated handling
of user events
When you want to create
a re-usable component
Often, both of these requirements will be present at the same time.
UselessApp.RandomEchoView = Ember.View.extend({
mouseMove : function(){
this.controller.set('random',Math.random())
}
});
These slides:
https://github.com/Octo-Labs/intro-to-ember
The stupidly simple app used in the slides:
https://github.com/Octo-Labs/stupid-simple-ember-app
The Lightning Scheduler app shown after the slides:
https://github.com/Octo-Labs/lightning-scheduler