Ember.Js and Rails — Playing Nicely
Hello! I’ve been in a whirlwind of trying out different technologies and just trying to expand my development skills as much as possible, and on this trip one of the things I tried out was using building an Ember App with a rails back end/api.
The app itself is just a simple CRM and the code can be found here https://github.com/YDOCHOUSE/ember-crm (or live here vast-plains-4143.herokuapp.com/, or the tutorial I based this app off of http://ember.vicramon.com/) . Basically this is my first journey into the world of ember so I’ll be giving my impressions and any gotchas I experienced along the way. Oh, I decided to try out emblem.js (http://emblemjs.com/) an alternative to handlebars. I’m going to be releasing this in parts as my first journey has taught me a lot and I want to share and explain what I can for your understanding, and especially mine!
One of the first things you’ll want do is simply include the appropriate gems, in my case:
gem ‘ember-rails’ (another gem called Ember-cli seems to be an alternative)
gem ‘ember-source’
gem ‘emblem-rails’ (if you plan to use emblem)
You’ll want to follow the github instructions of the gem to use all the appropriate generators to get things rolling. (and don’t forget to bundle!)
Well I’m on the details of setup, I just need to add you might want to grab the ember inspector as an add on to your console, it’s pretty invaluable for debugging and interesting to see all the transitions happening, as well as informative of how Ember works. It lets you view your data and their attributes right in the console, shows you all your routes, and gives you a tree-view which sort of works like the sources area in the dev console, it shows you what is rendered and where it came from.
The leg work of setting up Ember with Rails went incredibly smooth and I ran into no problems. (When does this ever happen in our lives, my fellow developers?!)
The Object Model:
So one of the first things I found out about is everything in Ember extends from one base object which is know as simply, Ember.Object. You can see this clearly throughout the app, even the views, and controllers, Ember.View, Ember.Controller, respectively, follow this rule. The basic Ember.Object, has it uses, things like creating service objects that handle some specific logic (like creating methods on models in rails).
Since everything in Ember extends from the basic Ember.Object, any time we want to create our own object “class” we simply call extend on Ember.Object , and then go about as we would creating a regular javascript object.
App.User = Ember.Object.extend(
isHuman: false,
isDalek: true
)
We pre-fix with App because Ember needs a variable to keep all data for the application in and Ember developers have come up with the convention of App as the variable name. Coming from a rails background, this is most welcome to me!
Along side with being able to have functions, objects may also have a special type of function you can declare as a computed property, meaning ember will automatically call the function and feed you the value. This really comes in handy when you want to grab from existing attributes on the object to manipulate the data in some way. A good example exists on the ember docs:
App.Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')
});
var ironMan = App.Person.create({
firstName: "Tony",
lastName: "Stark"
});
ironMan.get('fullName'); // "Tony Stark"Here they use it to call ironMan.get(‘fullname’); the property when called with get, executes the function and returns us the combined attributes of firstName and lastName. I can only imagine how useful this might become in future applications.
A second interesting interesting note about Ember.Objects is they have a property observer which can be set inside as a property itself. It’s job will be to watch the value of another property and fire off a function if it’s value changes. This can also be hooked up to a computer property. Amazing! Very cool functions. Back to the ember docs for a good example.
Person = Ember.Object.extend({
// these will be supplied by `create`
firstName: null,
lastName: null,
fullName: function() {
var firstName = this.get('firstName');
var lastName = this.get('lastName');
return firstName + ' ' + lastName;
}.property('firstName', 'lastName'),
fullNameChanged: function() {
// deal with the change
}.observes('fullName').on('init')
});
var person = Person.create({
firstName: 'Yehuda',
lastName: 'Katz'
});
person.set('firstName', 'Brohuda');Sadly, I don’t have any more time tonight, as I have a project I’m working on that I hear calling my name, but I will continue with this series tomorrow night, probably discussing routers in ember. I found the way in which it handles this particularly interesting!
Into night I go, allons-y! And don’t wait for me to tell your more about Ember if you’re curious, go at it!