Merge pull request #6463 from SylvainCorlay/bulk_update

Bulk update of widget attributes
This commit is contained in:
Jonathan Frederic 2014-09-22 13:49:16 -07:00
commit 6637b768cc

View File

@ -24,7 +24,7 @@ define(["widgets/js/manager",
this._buffered_state_diff = {};
this.pending_msgs = 0;
this.msg_buffer = null;
this.key_value_lock = null;
this.state_lock = null;
this.id = model_id;
this.views = [];
@ -80,15 +80,16 @@ define(["widgets/js/manager",
apply_update: function (state) {
// Handle when a widget is updated via the python side.
var that = this;
_.each(state, function(value, key) {
that.key_value_lock = [key, value];
try {
WidgetModel.__super__.set.apply(that, [key, that._unpack_models(value)]);
} finally {
that.key_value_lock = null;
}
});
this.state_lock = state;
try {
var that = this;
WidgetModel.__super__.set.apply(this, [Object.keys(state).reduce(function(obj, key) {
obj[key] = that._unpack_models(state[key]);
return obj;
}, {})]);
} finally {
this.state_lock = null;
}
},
_handle_status: function (msg, callbacks) {
@ -151,11 +152,13 @@ define(["widgets/js/manager",
// Delete any key value pairs that the back-end already knows about.
var attrs = (method === 'patch') ? options.attrs : model.toJSON(options);
if (this.key_value_lock !== null) {
var key = this.key_value_lock[0];
var value = this.key_value_lock[1];
if (attrs[key] === value) {
delete attrs[key];
if (this.state_lock !== null) {
var keys = Object.keys(this.state_lock);
for (var i=0; i<keys.length; i++) {
var key = keys[i];
if (attrs[key] === this.state_lock[key]) {
delete attrs[key];
}
}
}
@ -266,6 +269,19 @@ define(["widgets/js/manager",
}
},
on_atomic_change: function(keys, callback, context) {
// on__atomic_change(["key1", "key2"], foo, context) differs from
// on("change:key1 change:key2", foo, context).
// If the widget attributes key1 and key2 are both modified,
// the second form will result in foo being called twice
// while the first will call foo only once.
this.on('change', function() {
if (keys.some(this.hasChanged, this)) {
callback.apply(context);
}
}, this);
},
});
widgetmanager.WidgetManager.register_widget_model('WidgetModel', WidgetModel);