formModel.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Model that represents our form.
  3. *
  4. * @package Ninja Forms client
  5. * @copyright (c) 2017 WP Ninjas
  6. * @since 3.0
  7. */
  8. define( [], function() {
  9. var model = Backbone.Model.extend( {
  10. defaults: {
  11. objectType: 'form',
  12. id: 0,
  13. title: 'unknown',
  14. created_at: 'unknown'
  15. },
  16. url: function() {
  17. return ajaxurl + "?action=nf_forms&form_id=" + this.get( 'id' );
  18. },
  19. initialize: function() {
  20. this.set( 'id', Number( this.get( 'id' ) ) );
  21. if( this.get( 'id' ) ) {
  22. this.initShortcode( this.get( 'id' ) );
  23. }
  24. // Strip HTML tags from the form title.
  25. if ( this.get( 'title' ) ) {
  26. this.set( 'title', this.get( 'title' ).replace(/<\/?[^>]+(>|$)/g, "") );
  27. }
  28. },
  29. initShortcode: function( id ) {
  30. var shortcode = '[ninja_form id=' + id + ']';
  31. this.set( 'shortcode', shortcode);
  32. },
  33. /* Overwrite the standard backbone delete method
  34. * allowing us to send a POST request instead of DELETE
  35. */
  36. destroy: function() {
  37. var that = this;
  38. jQuery.ajax({
  39. type: "POST",
  40. url: ajaxurl + '?action=nf_forms&method_override=delete&form_id=' + this.get( 'id' ),
  41. success: function( response ){
  42. var response = JSON.parse( response );
  43. that.collection.remove( that );
  44. }
  45. });
  46. }
  47. } );
  48. return model;
  49. } );