Tag Cloud
Concourse CI/CD devops prometheus kubernetes monitoring modbus kepware c-programming IoT golang telegram bot python cli urwid elasticsearch aws ecs apache spark scala AWS EMR hadoop webhooks ssl nginx digital-ocean emr apache pig datapipeline found.io elastic-cloud rails try capybara docker capistrano heka bigquery kafka protobuf vim iterm javascript emberjs git scripting dnsmasq bem frontend meteorjs meteorite heroku

Emberjs Model RestAdapter Conventions

After I was done with exploring with local fixtures data, its time to experiment with using the RestAdapter provided by Emberjs to call the appropriate json data from the rails backend.

It was pretty simple and straightforward mostly. You basically just change the store to have “adapter: DS.RESTAdapter” and extend the model to have the appropriate attribute names from the server. (I’m using ActiveModel serializers) It then automagically works. Emberjs helps to figure out all the paths it needs to get the appropriate data.

So for example, if you bound the index/root “/” to the tasks model via a task router and set a nested resource for tasks in the same route.

App.Router.map(function() {
    this.resource(tasks, {path: /}, function() {
       this.resource(task, {path: :task_id});
    });
});

It’ll send a get request to /tasks when you’re at root. It will also know that when you are at #/2, it should be sending the request to /tasks/2 as long as you provide the task_id to the router like above.

One small thing that tripped me was the code below:

App.Task = DS.Model.extend({
   name: DS.attr('string'),
   task_details: DS.attr('string'),
   isCompleted: DS.attr('boolean')
});

How do you think the json data should look like? Like this?

{ name: 'test', task_details: 'details', isCompleted: false}

Apparently not so! It turns out that Emberjs expects snake case in the JSON when the attribute name is camelized! So the correct format that you should be sending from the server should be like this!

{ name: 'test', task_details: 'details', is_completed: false}
comments powered by Disqus