POST to server with custom URL - Help?


#1

I’m trying to connect with my server to pass a POST request in JSON.

The URL I send it to, however, depends on the id being viewed on the template. So if I’m looking at a “group” with the ID 1100, the URL should be http://localhost:3100/api/groups/1100/something/start.

I just can’t figure out how to get the group id in there. Trying, but no dice:

jQuery.ajax({
             url: "/api/groups/"+ params.group_id +"/something/start",
             type: 'post',
                 contentType: 'application/json',
                 dataType: 'json'
                      });
                      },

Some more useful info:

My route:

App.IndexRoute = Ember.Route.extend({                                                                                  
 sortProperties: ['id'],                                                                                              
 sortAscending: true,                                                                                                 

 actions: {                                                                                                           
toggleMenu: function() {                                                                                           
  this.controller.toggleProperty('menuVisible');                                                                   
  this.controller.pushBody();                                                                                      
}                                                                                                                  
  },                                                                                                                   

  model: function() {                                                                                                  
 var store = this.store;                                                                                            
var url = '/api/groups/top?subscribe=true';                                                   
    $.getJSON(url).then(function(data) {                                                                               
  store.pushMany('group', data.data.groups); 


});
 return this.store.all('group');                                                                                                                
  }

});     

Controller:

App.IndexController = Em.ArrayController.extend({
sortProperties: ['name'],
sortAscending: true,
 menuVisible: false,     
 actions: {
 poweron: function(){
     jQuery.ajax({
         url: "/api/groups/"+ params.group_id +"/something/start",
         type: 'post',
             contentType: 'application/json',
             dataType: 'json'
                  });
                  }

});

Thoughts? Thanks!

Any thoughts?

Thanks!


#2

Solved it…

Turned out I needed to pass along the id argument to the controller - how else would the controller know which id to use?

More - http://emberjs.com/guides/templates/actions/#toc_action-parameters

The correct action looks more like this:

actions: {
poweron: function(id){
       group_id = id;
       alert(group_id); //let's see what's being sent over
       
         jQuery.ajax({
             url: "/api/groups/"+ group_id +"/something/start",
             type: 'post',
    
 ........etc and so on.