0
votes

I currently have an ember model which uses promises and returns my data via a URL.

I have a controller action which deletes records from the same model. How can I request my model data after I have completed a controller action?

Currently the only way is if I use a SetTimeout and then a transitionTo which is not ideal.

My Controller:

App.SubmitterAllController = Ember.ObjectController.extend({ // initial value isExpanded: false,

actions: { deleteSubmitter: function(params) { // So we can access available functions inside nested functions me = this;

  var submitterStore =     [];
  var submittersToDelete = '';

  $('.jars-used ul li').each(function() {
    if ($(this).hasClass('selected')) {
      submitterStore.push('"'+ $(this).text().replace(/\.jar/, '') + '"');
    }
  });

  $.each(submitterStore, function(i, value) {
    submittersToDelete += value;
  });

  var urlFormulation = '/ProcessManager/manage?type=submitters&action=removeJar&jarsName=[' +

submittersToDelete +']';

 $.ajax({type: 'GET', url: urlFormulation, dataType: 'json', 
  success: function(jsonData) {
    $('#myModal').modal();
    $('#myModal .modal-body').html('You have successfully deleted the submitters: ' + submittersToDelete);

  }, error: function() {
    $('#myModal .modal-body').modal();
    $('#myModal .modal-body').html('Warning: there has been an error deleting these processes');
  }
});

}   } });

My Model:

App.SubmitterAllRoute = Ember.Route.extend({
  model: function(params) {
    return Ember.$.getJSON('/ProcessManager/manage?type=submitters&action=getSubmitters').then(function(data) {
      var submitters = [];

      $.each(data, function(i, item) {
        $.each(item, function(i, item) {
          $.each(item, function(i, item) {
            // Push each submitter into submitter array
            submitters.push(item);
          });
        });
      });

      return submitters;

    });
  }
});
1
How about a callback, which is triggered after you are done deleting records? Let me see some code so that I can help you properly :) - snrlx
There we go, so after my controller action has finished, how can I reload my model? Thanks for your help :) - user847623

1 Answers

0
votes

If you simply want to reload your model within a controller action, do it as follows:

App.SampleController = Ember.ObjectController.extend({
    actions: {
        someAction: function(){

            //your function here

            this.set('model', [insert your $.getJSON call here]);
        }
    }
});

However, this doesn't seem like necessarily the best way to do what you need given the code you provided. After deleting the record using your ajax call, you can then manual remove that record from the model without making another $.getJSON call. Just use this.get() and this.set() functions to manipulate the model as needed.