Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Last active December 19, 2015 09:19
Show Gist options
  • Save lahmatiy/5932422 to your computer and use it in GitHub Desktop.
Save lahmatiy/5932422 to your computer and use it in GitHub Desktop.
HOWTO: Do you know that you can easily create dataset index fields for nested datasets for Entity?
basis.require('basis.entity');
basis.require('basis.data.index');
// create type with single field, that used for EntitySet
var Item = basis.entity.createType('SomeType', {
value: Number
});
// main Entity type
var DemoType = basis.entity.createType('DemoType', {
items: basis.entity.createSetType(Item), // nestes dataset
sum: basis.entity.calc('items', function(items){ // key moment here, we create calc field that
if (items) // create index on items property
return basis.data.index.sum(items, 'data.value');
})
});
// create instance of DemoType
var dataObject = DemoType({
items: [
{ value: 1 },
{ value: 2 },
{ value: 3 }
]
});
// dataObject.data.sum -> instance of basis.data.index.Sum
// dataObject.data.sum.value === 6
// demo view to show how bind index value to template
// as you can see no difference for simple cases because templates
var view = new basis.ui.Node({
container: document.body,
delegate: dataObject,
template: '<span>Items sum is {sum}</span>',
binding: {
sum: 'data:'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment