Guest User

Untitled

a guest
Jul 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // Top level UI namespace
  2.  
  3. var UI = {};
  4.  
  5. UI.PropertyEditor = Backbone.View.extend({
  6. events: {
  7. 'submit form': 'newValue',
  8. 'click a.remove': 'removeValue'
  9. },
  10.  
  11. initialize: function(options) {
  12. var that = this;
  13. this._values = options.values;
  14. this._unique = options.unique;
  15.  
  16. // Re-render on every change
  17. this.bind('changed', function() {
  18. that.render();
  19. });
  20.  
  21. // Finally, render
  22. this.render();
  23. },
  24.  
  25. // Add a new value for the property
  26. newValue: function(e) {
  27. var val = this.$('input[name=new_value]').val();
  28.  
  29. if (!_.include(this._values, val)) {
  30. if (this._unique) { // overwrite in case of unique values
  31. this.values = [val];
  32. } else {
  33. this._values.push(val);
  34. }
  35. this.trigger('changed');
  36. this.render(); // re-render
  37. } else {
  38. this.trigger('error:alreadyexists');
  39. }
  40. return false;
  41. },
  42.  
  43. // Remove a certain value
  44. removeValue: function(e) {
  45. var val = $(e.target).attr('value');
  46. this._values = _.reject(this._values, function(v) { return v === val; });
  47. this.trigger('changed');
  48. return false;
  49. },
  50.  
  51. // Get the current set of values
  52. values: function() {
  53. return this._values;
  54. },
  55.  
  56. // Render the editor, including the display of values
  57. render: function() {
  58. $(this.el).html(_.renderTemplate('property_editor', {
  59. values: this._values
  60. }));
  61. }
  62. });
Add Comment
Please, Sign In to add comment