Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make arbitrary API calls using Ember.js?

Tags:

rest

ember.js

What I'm trying to do is described in great detail here:

Call a Server-side Method on a Resource in a RESTful Way

I have Ember Data's RESTAdapter working with my API, but now I want to give Ember.js a way to kick off various server-side actions using custom routes, such as /docs/1/share or /docs/1/activate. The former would possibly modify the record but the latter would not.

What's the best way to do this?

TIA!

like image 923
hourback Avatar asked Oct 21 '14 15:10

hourback


2 Answers

Ember has jQuery baked in. In your controller:

actions: {
    activate: function() {
        var docId= this.get('id'), self= this;
        Ember.$.ajax({
            url: '/docs/%@/activate'.fmt(docId),
            // your other details...
        }).then(function(resolve) {
            self.set('name', resolve.doc.name);
            // process the result...
        });
    }
}
like image 185
Steve H. Avatar answered Sep 17 '22 18:09

Steve H.


You can also use ic-ajax which is a nice wrapper around jQuery.ajax, you can see an example here using ix-ajax.

like image 35
Adolfo Builes Avatar answered Sep 19 '22 18:09

Adolfo Builes