serviceModel.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. define( [], function() {
  2. var model = Backbone.Model.extend( {
  3. defaults: {
  4. objectType: 'service',
  5. name: '',
  6. slug: '',
  7. installPath: '',
  8. description: '',
  9. enabled: null,
  10. infoLink: null,
  11. serviceLink: null,
  12. is_installing: false,
  13. classes: ''
  14. },
  15. url: function() {
  16. return ajaxurl + "?action=nf_service_" + this.get( 'slug' );
  17. },
  18. /*
  19. * - Check for "Success"/Setup modal.
  20. * - (Maybe) Auto-redirect to the service.
  21. */
  22. initialize: function() {
  23. // Check for successful setup.
  24. if( this.get( 'slug' ) == serviceSuccess && this.get( 'successMessage' ) ){
  25. new jBox( 'Modal', {
  26. width: 300,
  27. addClass: 'dashboard-modal',
  28. overlay: true,
  29. closeOnClick: true,
  30. content: this.get( 'successMessage' ),
  31. title: this.get( 'successMessageTitle' ),
  32. closeButton: 'box'
  33. } ).open();
  34. }
  35. // Auto-redirect to the serviceLink on install.
  36. var that = this;
  37. nfRadio.channel( 'dashboard' ).reply( 'install:service:' + this.get( 'slug' ), function(){
  38. // If no service link url, then no need to auto-redirect.
  39. if( ! that.get( 'serviceLink' ) ) return;
  40. if( ! that.get( 'serviceLink' ).href ) return;
  41. var redirect = that.get( 'serviceLink' ).href;
  42. // Display a redirect notice.
  43. new jBox( 'Modal', {
  44. width: 300,
  45. addClass: 'dashboard-modal',
  46. overlay: true,
  47. closeOnClick: 'body',
  48. content: nfi18n.serviceRedirect,
  49. } ).open();
  50. // Trigger a redirect, where depends on the connected status.
  51. var oauth = nfRadio.channel( 'dashboard' ).request( 'get:oauth' );
  52. if( ! oauth.get( 'connected' ) ){
  53. if( that.get( 'connect_url' ) ){
  54. return window.location = that.get( 'connect_url' ) + '&redirect=' + redirect;
  55. }
  56. window.location = oauth.get( 'connect_url' ) + '&redirect=' + redirect;
  57. } else {
  58. window.location = redirect;
  59. }
  60. } );
  61. },
  62. /*
  63. * Sync the server with the model.
  64. */
  65. save: function() {
  66. var that = this;
  67. jQuery.ajax({
  68. type: "POST",
  69. url: this.url(),
  70. data: this.toJSON()
  71. }).done( function( response ){
  72. var data = JSON.parse( response );
  73. if( 'undefined' !== typeof data.error ) {
  74. alert( nfi18n.serviceUpdateError + ' ' + data.error );
  75. that.set( 'enabled', ! that.get( 'enabled' ) );
  76. }
  77. nfRadio.channel( 'dashboard').trigger( 'save:service-' + that.get( 'slug' ) );
  78. });
  79. }
  80. } );
  81. return model;
  82. } );