servicesController.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. define([ 'models/serviceCollection' ], function( ServiceCollection ) {
  2. var controller = Marionette.Object.extend( {
  3. initialize: function() {
  4. this.services = new ServiceCollection();
  5. nfRadio.channel( 'dashboard' ).reply( 'install:service', this.installService, this );
  6. nfRadio.channel( 'dashboard' ).reply( 'get:services', this.getServices, this );
  7. this.fetchServices();
  8. },
  9. getServices: function() {
  10. return this.services;
  11. },
  12. /*
  13. * Fetch services, with an optional callback function.
  14. */
  15. fetchServices: function( callback ) {
  16. this.services.fetch({
  17. success: function( model ){
  18. if( callback ) callback( model );
  19. nfRadio.channel( 'dashboard' ).trigger( 'fetch:services' );
  20. }
  21. });
  22. },
  23. /*
  24. * Request the remote install of the service's corresponding plugin.
  25. */
  26. installService: function( serviceModel ) {
  27. var that = this;
  28. if ( ! ( serviceModel instanceof Backbone.Model ) ) {
  29. var serviceModel = this.services.find( function( model ) {
  30. return serviceModel == model.get( 'slug' );
  31. });
  32. }
  33. serviceModel.set( 'is_installing', true );
  34. var slug = serviceModel.get( 'slug' );
  35. var installPath = serviceModel.get( 'installPath' );
  36. // Request to Install the service plugin.
  37. jQuery.post( ajaxurl, { action: 'nf_services_install', plugin: slug, install_path: installPath }, function( response ){
  38. that.fetchServices(function(){
  39. nfRadio.channel( 'dashboard' ).request( 'install:service:' + slug );
  40. });
  41. } );
  42. }
  43. });
  44. return controller;
  45. } );