##// 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 cell = this.last_modified_view.cell;
75 cell = this.last_modified_view.cell;
76 }
76 }
77 var callbacks = this._make_callbacks(cell);
77 var callbacks = this._make_callbacks(cell);
78 var data = {custom_content: content};
78 var data = {'custom_content': content};
79 this.comm.send(data, callbacks);
79 this.comm.send(data, callbacks);
80 },
80 },
81
81
@@ -25,11 +25,7 b' define(["notebook/js/widget"], function(widget_manager){'
25 render : function(){
25 render : function(){
26 var that = this;
26 var that = this;
27 this.setElement($("<button />")
27 this.setElement($("<button />")
28 .addClass('btn')
28 .addClass('btn'));
29 .click(function() {
30 that.model.set('clicks', that.model.get('clicks') + 1);
31 that.model.update_other_views(that);
32 }));
33
29
34 this.update(); // Set defaults.
30 this.update(); // Set defaults.
35 },
31 },
@@ -48,7 +44,15 b' define(["notebook/js/widget"], function(widget_manager){'
48
44
49 return IPython.WidgetView.prototype.update.call(this);
45 return IPython.WidgetView.prototype.update.call(this);
50 },
46 },
47
48 events: {
49 'click': '_handle_click',
50 },
51
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 widget_manager.register_widget_view('ButtonView', ButtonView);
58 widget_manager.register_widget_view('ButtonView', ButtonView);
@@ -27,7 +27,6 b' from IPython.utils.traitlets import Unicode, Dict, List, Instance, Bool'
27 from IPython.display import Javascript, display
27 from IPython.display import Javascript, display
28 from IPython.utils.py3compat import string_types
28 from IPython.utils.py3compat import string_types
29
29
30
31 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
32 # Classes
31 # Classes
33 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
@@ -114,7 +113,7 b' class Widget(LoggingConfigurable):'
114 def _handle_msg(self, msg):
113 def _handle_msg(self, msg):
115 """Called when a msg is recieved from the frontend"""
114 """Called when a msg is recieved from the frontend"""
116 data = msg['content']['data']
115 data = msg['content']['data']
117
116
118 # Handle backbone sync methods CREATE, PATCH, and UPDATE
117 # Handle backbone sync methods CREATE, PATCH, and UPDATE
119 if 'sync_method' in data and 'sync_data' in data:
118 if 'sync_method' in data and 'sync_data' in data:
120 sync_method = data['sync_method']
119 sync_method = data['sync_method']
@@ -384,7 +383,6 b' class Widget(LoggingConfigurable):'
384 ----------
383 ----------
385 view_name: unicode (optional)
384 view_name: unicode (optional)
386 View to display in the frontend. Overrides default_view_name."""
385 View to display in the frontend. Overrides default_view_name."""
387
388 if not view_name:
386 if not view_name:
389 view_name = self.default_view_name
387 view_name = self.default_view_name
390
388
@@ -28,15 +28,16 b' class ButtonWidget(Widget):'
28 default_view_name = Unicode('ButtonView')
28 default_view_name = Unicode('ButtonView')
29
29
30 # Keys
30 # Keys
31 _keys = ['clicks', 'description', 'disabled']
31 _keys = ['description', 'disabled']
32 clicks = Int(0, help="Number of times the button has been clicked.")
33 description = Unicode('', help="Description of the button (label).")
32 description = Unicode('', help="Description of the button (label).")
34 disabled = Bool(False, help="Enable or disable user changes.")
33 disabled = Bool(False, help="Enable or disable user changes.")
35
34
36
35
37 def __init__(self, **kwargs):
36 def __init__(self, **kwargs):
38 self._click_handlers = []
39 super(ButtonWidget, self).__init__(**kwargs)
37 super(ButtonWidget, self).__init__(**kwargs)
38
39 self._click_handlers = []
40 self.on_msg(self._handle_button_msg)
40
41
41
42
42 def on_click(self, callback, remove=False):
43 def on_click(self, callback, remove=False):
@@ -57,24 +58,36 b' class ButtonWidget(Widget):'
57 self._click_handlers.append(callback)
58 self._click_handlers.append(callback)
58
59
59
60
60 def _clicks_changed(self, name, old, new):
61 def _handle_button_msg(self, content):
61 """Handles when the clicks property has been changed. Fires on_click
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 callbacks when appropriate."""
74 callbacks when appropriate."""
63 if new > old:
75
64 for handler in self._click_handlers:
76 for handler in self._click_handlers:
65 if callable(handler):
77 if callable(handler):
66 argspec = inspect.getargspec(handler)
78 argspec = inspect.getargspec(handler)
67 nargs = len(argspec[0])
79 nargs = len(argspec[0])
68
80
69 # Bound methods have an additional 'self' argument
81 # Bound methods have an additional 'self' argument
70 if isinstance(handler, types.MethodType):
82 if isinstance(handler, types.MethodType):
71 nargs -= 1
83 nargs -= 1
72
84
73 # Call the callback
85 # Call the callback
74 if nargs == 0:
86 if nargs == 0:
75 handler()
87 handler()
76 elif nargs == 1:
88 elif nargs == 1:
77 handler(self)
89 handler(self)
78 else:
90 else:
79 raise TypeError('ButtonWidget click callback must ' \
91 raise TypeError('ButtonWidget click callback must ' \
80 'accept 0 or 1 arguments.')
92 'accept 0 or 1 arguments.')
93
General Comments 0
You need to be logged in to leave comments. Login now