Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lahmatiy/b8b2289881244aa79c93 to your computer and use it in GitHub Desktop.
Save lahmatiy/b8b2289881244aa79c93 to your computer and use it in GitHub Desktop.
basis.data.ReadOnlyDataset#contains
// we could create several effects for node/models and don't affect node/models
var source = new basis.data.Dataset({ items: [/* .. */]});
var modified = new basis.data.dataset.Filter({
source: source,
ruleEvents: 'rollbackUpdate',
rule: 'data.modified'
});
var customSet = new basis.data.Dataset();
var view = new basis.ui.Node({
dataSource: items,
childClass: {
binding: {
modified: function(node){ // w/o contains we should listen for delegateChanged/targetChanged
return modified.contains(node); // and listen delegate/target for rollbackUpdate
}, // and call updateBind('modified') on any event
custom: customSet.contains.bind(customSet), // w/o contains we should add handler to customSet
// on itemsChanged event, and use 2 loops for inserted and deleted items
// and updateBind('custom'), also we should call this action once for
// custom.getItems()
notCustom: function(node){ // otherwise, we need to do somthing like for custom, but invert rule
return customSet // or use Subtract
.contains(node)
.as(basis.bool.invert);
}
}
}
});
// selection alternative
// don't affect model or node
var item = new basis.data.Datgaset({ items: /* .. */ });
var selected = new basis.data.Dataset();
var view = new basis.ui.Node({
dataSource: items,
childClass: {
binding: {
selected: {
events: 'delegateChanged',
getter: function(node){
return selected.contains(node.delegate);
}
}
},
action: {
select: function(){
if (selected.has(this.delegate))
selected.remove(this.delegate);
else
selected.add(this.delegate);
}
}
}
});
var CONTAINS_HANLDER = {
destroy: function(sender){
var id = sender.basisObjectId;
var value = this.contains_[id];
delete this.contains_[id];
value.destroy();
}
};
basis.data.ReadOnlyDataset.prototype.emit_itemsChanged = createEvent('itemsChanged', 'delta') && function(delta){
var items;
var insertCount = 0;
var deleteCount = 0;
var object;
// add new items
if (items = delta.inserted)
{
for (var i = 0; i < items.length; i++)
{
var object = items[i];
var id = object.basisObjectId;
this.items_[id] = object;
if (this.contains_ && id in this.contains_)
this.contains_[id].set(true);
insertCount++;
}
}
// remove old items
if (items = delta.deleted)
{
for (var i = 0; i < items.length; i++)
{
var object = items[i];
var id = object.basisObjectId;
delete this.items_[id];
if (this.contains_ && id in this.contains_)
this.contains_[id].set(false);
deleteCount++;
}
}
// update item count
this.itemCount += insertCount - deleteCount;
// drop cache
this.cache_ = insertCount == this.itemCount ? delta.inserted : null;
// call event
events.itemsChanged.call(this, delta);
};
basis.data.ReadOnlyDataset.prototype.contains = function(object){
if (object instanceof DataObject == false)
{
/** @cut */ basis.dev.warn('Wrong data type: object should be an instance of basis.data.Object');
return false;
}
if (!this.contains_)
this.contains_ = {};
var id = object.basisObjectId;
var value = this.contains_[id];
if (!value)
{
object.addHandler(CONTAINS_HANLDER, this);
value = this.contains_[id] = new Value({
value: this.has(object)
});
}
return this.contains_[id];
};
basis.data.ReadOnlyDataset.prototype.destroy = function(){
//...
var contains = this.contains_;
if (contains)
{
for (var id in contains)
contains[id].destroy();
this.contains_ = null;
}
// ..
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment