Sunday, August 9, 2015

Using ES6 / ES2015 with Meteor 1.1.0.3

Mission

Meteor 1.2 is set to ship later this summer.  It will have built-in support for ES2015 (along with support for Angular and React).  I'm really looking forward to this release but I want to start using ES2015 today.  These are my tips on how to get things (mostly) working.

Meteor packages

Most documentation will tell you to add the ecmascript package but it doesn't seem to install just yet.  The following error message appears on both Windows and OSX:


Instead we'll add the Babel package:

meteor add grigio:babel

With Babel (pronounced bay•bull) we can now use ES2015 syntax in our project with the caveat that we have to suffix our files correctly.  The babel package is going to look for files that end in either .es6, es6.js or .jsx).

Using classes

Normally when you declare a class in ES2015 the syntax looks like this:
class Gamer {
   ...
}
Due to the way Meteor bundles files and how variables are scoped you have to this:
Gamer = class Gamer {
   ...
}
Then you'll be able to new up a Gamer object in other files.

Some gotchas

I haven't tested out all ES2015 features but one of the features I couldn't get to work is getters and setters.  At this point, I've just accepted that it's not supported until Meteor 1.2 is released.
class Gamer {
   get name() { return this.name; }
}

Testing with Velocity

Velocity is the defacto test runner to use with Meteor. Depending on which 'reporting' package you install for it, you'll see the results of your tests either in the console or in the browser.  Read more about Velocity here.

There are 2 packages we'll want to install
meteor add sanjo:jasmine
meteor add velocity:html-reporter

I write my tests using Jasmine.  When you use Velocity with Jasmine you need to group your tests under the correct folder structure (under the root of your app):
  1. tests/jasmine/client/unit
  2. tests/jasmine/client/integration
  3. tests/jasmine/server/unit
  4. tests/jasmine/server/integration
It seems that only tests found in 1 and 2 above have support for Babel.  Server tests will not work with it.


Hopefully this will help some of you folks that are hoping to use ES2015 features within Meteor and have hit a wall recently.  I know I did.




Wednesday, July 8, 2015

Getting Angular's $http.post method working with Web API

By default, when you deploy a .Net Web API project, you are not allowed to make cross site requests to it from, say, an angular application.  The browser will prevent the call from happening because your api needs to send the proper response header to allow the call to be made.

In order to enable it, you need to make a modification to your web.config file.  Enter the following somewhere between your system.webServer tags:
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>
Also, in your ApiController, add an empty Options method (some browsers issue this command to check the validity of a cross origin request):
    public class DataController : ApiController
    {
        public void Options() { }
    }


Friday, January 30, 2015

Getting Docker working on Windows 8.1

1. Installation

Download and run the latest installer:  https://github.com/boot2docker/windows-installer/releases

Pick all 3 items to install unless you know that you already have them on your machine.




2. Enable virtualization

Reboot your computer and go into the BIOS settings.  Find and enable Virtualization which should look something like the image below:




3. Make sure VirtualBox can launch the VM

Launch Oracle VM VirtualBox and select the boot2docker-vm image.
Click Settings -> System.
Make sure the RAM is set to 4096 MB (or 2048 if you can't give it that much).
Click the Start button to start the VM and see if you get any errors.




4. Launch Boot2Docker

If everything went well you should be prompted to enter the password for docker@localhost.  The password is 'tcuser'
For some reason, I was prompted repeatedly until I reached the linux console.


Eventually you should reach the console prompt, then you're ready to start using Docker!




For further reading go here (by maximheckel) for details on running docker and sharing folders (scroll down to Running Docker containers):

http://blog.tutum.co/2014/11/05/how-to-use-docker-on-windows/