Skip to main content

EmberJS development using the esapiserver

I started out using the fixture and local storage adapters in EmberJS, but experienced that the limitations that these adapters have, would turn out be a pain later when it was time to release my app - I wanted to make sure that what I test, was consistant with what I would release, hence the esapiserver.

Installation

Install the gem
    $ gem install esapiserver

Start up your mongoDB server   
    $ mongoD

Start the Ember Sinatra/MongoDB API server
    $ easapiserver

EmberJS

To get EmberJS and the esapiserver to play together, we need to configure our RESTAdapter
App.ApplicationAdapter = DS.RESTAdapter.extend
    namespace: 'api'
    host: 'http://127.0.0.1:4567'
    corsWithCredentials: true
Because we are using an "external server", we have to enable CORS support in EmberJS. (more about that here)
Now, lets assume that we have the following model setup in EmberJS:
App.Post = DS.Model.extend
    content: DS.attr('string')
    comments: DS.hasMany('strategy',
        async: true
    )

App.comment = DS.Model.extend
    content: DS.attr('string')
    post: DS.belongsTo('post',
        embedded: true
    )
creating our record,
post = @store.createRecord("post",
    content: 'EmberJS development'
)

post.save().then ->
    comment = store.createRecord("comment",
        content: 'the first comment'
        post: post
    )

    comment.save().then ->
        post.get("comments").pushObject(comment)
        post.save()
and then making a GET request to the esapiserver,
@store.find('post', "53b9a08f19cfd220bc000001")
will return the following EmberJS friendly JSON
{
    post: [
        {
            content: "EmberJS development",
            comments: [
                "53e3857219cfd21340000064"
            ],
            id: "53b9a08f19cfd220bc000001"
        }
    ]
}

In the next installment I will focus on updating and deleting records.

Popular posts from this blog

Automatic Handlebars template compilation in WebStorm

Using WebStorm with EmberJS, I needed to precompile my Handlebars templates on the go. The Handlebars precompiler works, but using it with EmberJS requires some additional modifications, and getting it to work with directories seemed to be a bit problematic(a wildcard File Watchers issue in WebStorm). I tried to install Ember-Precompile but, being currently on Windows, I gave up because of node-gyp.js rebuild issues. Eventually I found the excellent ember-template-compiler module that did the trick for me.
Setup There are a couple of dependencies that needs to be installed to get this to work:

WebStorm and NodeJS of course.EmberJS - this is not actually a dependency, but definitely a requirement.Handlebars@1.1 - an ember-template-compiler dependency.Walk - used to traverse the directories where the templates are situated. My template file structure looks like the following:

I needed to compile those templates into a single templates.js file to be included in my project. To do that I …