Changed button to use custom messages instead of state to communicate events.

This commit is contained in:
Jonathan Frederic 2013-11-21 23:44:49 +00:00
parent c299022cc7
commit 10002f5a81
4 changed files with 45 additions and 30 deletions

View File

@ -75,7 +75,7 @@ define(["components/underscore/underscore-min",
cell = this.last_modified_view.cell;
}
var callbacks = this._make_callbacks(cell);
var data = {custom_content: content};
var data = {'custom_content': content};
this.comm.send(data, callbacks);
},

View File

@ -25,11 +25,7 @@ define(["notebook/js/widget"], function(widget_manager){
render : function(){
var that = this;
this.setElement($("<button />")
.addClass('btn')
.click(function() {
that.model.set('clicks', that.model.get('clicks') + 1);
that.model.update_other_views(that);
}));
.addClass('btn'));
this.update(); // Set defaults.
},
@ -48,7 +44,15 @@ define(["notebook/js/widget"], function(widget_manager){
return IPython.WidgetView.prototype.update.call(this);
},
events: {
'click': '_handle_click',
},
_handle_click: function(){
this.model.last_modified_view = this; // For callbacks.
this.model.send({event: 'click'});
},
});
widget_manager.register_widget_view('ButtonView', ButtonView);

View File

@ -27,7 +27,6 @@ from IPython.utils.traitlets import Unicode, Dict, List, Instance, Bool
from IPython.display import Javascript, display
from IPython.utils.py3compat import string_types
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
@ -114,7 +113,7 @@ class Widget(LoggingConfigurable):
def _handle_msg(self, msg):
"""Called when a msg is recieved from the frontend"""
data = msg['content']['data']
# Handle backbone sync methods CREATE, PATCH, and UPDATE
if 'sync_method' in data and 'sync_data' in data:
sync_method = data['sync_method']
@ -384,7 +383,6 @@ class Widget(LoggingConfigurable):
----------
view_name: unicode (optional)
View to display in the frontend. Overrides default_view_name."""
if not view_name:
view_name = self.default_view_name

View File

@ -28,15 +28,16 @@ class ButtonWidget(Widget):
default_view_name = Unicode('ButtonView')
# Keys
_keys = ['clicks', 'description', 'disabled']
clicks = Int(0, help="Number of times the button has been clicked.")
_keys = ['description', 'disabled']
description = Unicode('', help="Description of the button (label).")
disabled = Bool(False, help="Enable or disable user changes.")
def __init__(self, **kwargs):
self._click_handlers = []
super(ButtonWidget, self).__init__(**kwargs)
self._click_handlers = []
self.on_msg(self._handle_button_msg)
def on_click(self, callback, remove=False):
@ -57,24 +58,36 @@ class ButtonWidget(Widget):
self._click_handlers.append(callback)
def _clicks_changed(self, name, old, new):
"""Handles when the clicks property has been changed. Fires on_click
def _handle_button_msg(self, content):
"""Hanlde a msg from the front-end
Parameters
----------
content: dict
Content of the msg."""
if 'event' in content and content['event'] == 'click':
self._handle_click()
def _handle_click(self):
"""Handles when the button has been clicked. Fires on_click
callbacks when appropriate."""
if new > old:
for handler in self._click_handlers:
if callable(handler):
argspec = inspect.getargspec(handler)
nargs = len(argspec[0])
for handler in self._click_handlers:
if callable(handler):
argspec = inspect.getargspec(handler)
nargs = len(argspec[0])
# Bound methods have an additional 'self' argument
if isinstance(handler, types.MethodType):
nargs -= 1
# Bound methods have an additional 'self' argument
if isinstance(handler, types.MethodType):
nargs -= 1
# Call the callback
if nargs == 0:
handler()
elif nargs == 1:
handler(self)
else:
raise TypeError('ButtonWidget click callback must ' \
'accept 0 or 1 arguments.')
# Call the callback
if nargs == 0:
handler()
elif nargs == 1:
handler(self)
else:
raise TypeError('ButtonWidget click callback must ' \
'accept 0 or 1 arguments.')