##// END OF EJS Templates
Changed button to use custom messages instead of state to communicate events.
Jonathan Frederic -
Show More
@@ -75,7 +75,7 b' define(["components/underscore/underscore-min",'
75 75 cell = this.last_modified_view.cell;
76 76 }
77 77 var callbacks = this._make_callbacks(cell);
78 var data = {custom_content: content};
78 var data = {'custom_content': content};
79 79 this.comm.send(data, callbacks);
80 80 },
81 81
@@ -25,11 +25,7 b' define(["notebook/js/widget"], function(widget_manager){'
25 25 render : function(){
26 26 var that = this;
27 27 this.setElement($("<button />")
28 .addClass('btn')
29 .click(function() {
30 that.model.set('clicks', that.model.get('clicks') + 1);
31 that.model.update_other_views(that);
32 }));
28 .addClass('btn'));
33 29
34 30 this.update(); // Set defaults.
35 31 },
@@ -49,6 +45,14 b' define(["notebook/js/widget"], function(widget_manager){'
49 45 return IPython.WidgetView.prototype.update.call(this);
50 46 },
51 47
48 events: {
49 'click': '_handle_click',
50 },
51
52 _handle_click: function(){
53 this.model.last_modified_view = this; // For callbacks.
54 this.model.send({event: 'click'});
55 },
52 56 });
53 57
54 58 widget_manager.register_widget_view('ButtonView', ButtonView);
@@ -27,7 +27,6 b' from IPython.utils.traitlets import Unicode, Dict, List, Instance, Bool'
27 27 from IPython.display import Javascript, display
28 28 from IPython.utils.py3compat import string_types
29 29
30
31 30 #-----------------------------------------------------------------------------
32 31 # Classes
33 32 #-----------------------------------------------------------------------------
@@ -384,7 +383,6 b' class Widget(LoggingConfigurable):'
384 383 ----------
385 384 view_name: unicode (optional)
386 385 View to display in the frontend. Overrides default_view_name."""
387
388 386 if not view_name:
389 387 view_name = self.default_view_name
390 388
@@ -28,16 +28,17 b' class ButtonWidget(Widget):'
28 28 default_view_name = Unicode('ButtonView')
29 29
30 30 # Keys
31 _keys = ['clicks', 'description', 'disabled']
32 clicks = Int(0, help="Number of times the button has been clicked.")
31 _keys = ['description', 'disabled']
33 32 description = Unicode('', help="Description of the button (label).")
34 33 disabled = Bool(False, help="Enable or disable user changes.")
35 34
36 35
37 36 def __init__(self, **kwargs):
38 self._click_handlers = []
39 37 super(ButtonWidget, self).__init__(**kwargs)
40 38
39 self._click_handlers = []
40 self.on_msg(self._handle_button_msg)
41
41 42
42 43 def on_click(self, callback, remove=False):
43 44 """Register a callback to execute when the button is clicked. The
@@ -57,10 +58,21 b' class ButtonWidget(Widget):'
57 58 self._click_handlers.append(callback)
58 59
59 60
60 def _clicks_changed(self, name, old, new):
61 """Handles when the clicks property has been changed. Fires on_click
61 def _handle_button_msg(self, content):
62 """Hanlde a msg from the front-end
63
64 Parameters
65 ----------
66 content: dict
67 Content of the msg."""
68 if 'event' in content and content['event'] == 'click':
69 self._handle_click()
70
71
72 def _handle_click(self):
73 """Handles when the button has been clicked. Fires on_click
62 74 callbacks when appropriate."""
63 if new > old:
75
64 76 for handler in self._click_handlers:
65 77 if callable(handler):
66 78 argspec = inspect.getargspec(handler)
@@ -78,3 +90,4 b' class ButtonWidget(Widget):'
78 90 else:
79 91 raise TypeError('ButtonWidget click callback must ' \
80 92 'accept 0 or 1 arguments.')
93
General Comments 0
You need to be logged in to leave comments. Login now