##// 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 },
@@ -48,7 +44,15 b' define(["notebook/js/widget"], function(widget_manager){'
48 44
49 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 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 #-----------------------------------------------------------------------------
@@ -114,7 +113,7 b' class Widget(LoggingConfigurable):'
114 113 def _handle_msg(self, msg):
115 114 """Called when a msg is recieved from the frontend"""
116 115 data = msg['content']['data']
117
116
118 117 # Handle backbone sync methods CREATE, PATCH, and UPDATE
119 118 if 'sync_method' in data and 'sync_data' in data:
120 119 sync_method = data['sync_method']
@@ -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,15 +28,16 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)
38
39 self._click_handlers = []
40 self.on_msg(self._handle_button_msg)
40 41
41 42
42 43 def on_click(self, callback, remove=False):
@@ -57,24 +58,36 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:
64 for handler in self._click_handlers:
65 if callable(handler):
66 argspec = inspect.getargspec(handler)
67 nargs = len(argspec[0])
68
69 # Bound methods have an additional 'self' argument
70 if isinstance(handler, types.MethodType):
71 nargs -= 1
72
73 # Call the callback
74 if nargs == 0:
75 handler()
76 elif nargs == 1:
77 handler(self)
78 else:
79 raise TypeError('ButtonWidget click callback must ' \
80 'accept 0 or 1 arguments.')
75
76 for handler in self._click_handlers:
77 if callable(handler):
78 argspec = inspect.getargspec(handler)
79 nargs = len(argspec[0])
80
81 # Bound methods have an additional 'self' argument
82 if isinstance(handler, types.MethodType):
83 nargs -= 1
84
85 # Call the callback
86 if nargs == 0:
87 handler()
88 elif nargs == 1:
89 handler(self)
90 else:
91 raise TypeError('ButtonWidget click callback must ' \
92 'accept 0 or 1 arguments.')
93
General Comments 0
You need to be logged in to leave comments. Login now