Crossfilter create dimension with data whose have nested objects - dc.js

I'm trying to create graphics with the libraries crossfilter, d3 and dc, and it works well until I create the dimension with an array of objects like this:
{ age: 12, name: 'John Doe', ocupation: 'developer' }
But when in the data I use nested objects and create dimentions with them it wont works to me.
Nested object example:
{ age: 12, name: 'John Doe', nested: { value: 'developer' } }
I don't find any docs about how to use crossfilter with nested objects, then is it possible?
How can I do it?

You define the accessor functions for crossfilter, so you can define them however you like.
For example, if you're trying to create an occupation dimension, you could do
var occDimension = cf.dimension(function(row) {
return row.nested.value;
})
If this was not your question, you'll need to provide more details and an example of the code which didn't work.

Related

Filter documents based on value of an attribute inside an array of objects

RethinkDB newb here and I can't figure this one out.
Lets say I have a table named mydata with documents that have the following basic structure:
{
"SomeAttirbute": "SomeValue",
"team": [
{
"name": "john" ,
"other": "stuff",
} ,
{
"name": "jane" ,
"other": "junk",
}
] ,
...
}
How do I get all documents in the mydata table that have john for a value of the name attribute for any of the elements in the team array?
This is pretty easy and requires a simple ReQL expression. In JavaScript it would be something like this:
const name = 'john';
...
r.db('q50732045')
.table('mydata')
// The predicate below can be literally read as:
// a document whose `team` property is a sequence
// that contains any element with a property `name`
// that equals the given name
.filter(doc => doc('team').contains(member => member('name').eq(name)))
// No need to invoke the run method in Data Explorer
;
I do believe it can be easily re-written in Python.
I think this is what you are looking for:
r.db(insert_database_name).table("mydata").filter(
lambda doc: doc["team"]["name"].contains("john")
).run(con)
or:
r.db(insert_database_name).table("mydata").filter(
r.row["team"]["name"].contains("john")
).run(con)

Extjs: What is the correct way to use Associate Data of a model's field reference?

The Ext.data.Model class represents the backend models. And just like in the server code, some of its fields can be of another declared model type via the reference property. I've found out that using a model's getAssociatedData() function returns an object with all those referenced fields. However they only contain the reference object's data object they are not full fledged initialized Ext.data.Models, which forces a primitive object access and there is no way to use the model's configured proxies etc for loading/saving. Is this the correct/only way of using this functionality? We've also been looking for a way to add columns from referenced fields on a grid but it doesn't seem to work... I'm starting to doubt the usefulness of declaring referenced fields.
Example code:
Ext.define('MyApp.ModelA', {
extend: 'Ext.data.Model',
fields: [{
name: 'modelb',
reference: 'MyApp.ModelB'
}]
});
Ext.define('MyApp.ModelB', {
extend: 'Ext.data.Model',
fields: [{
name: 'modelId',
type: 'int'
}]
});
//...
var modelA = new MyApp.ModelA().load();
var modelB = modelA.getAssociatedData().modelb; //This is the only way to access it.
var modelBId = modelB.get('modelId') //This returns undefined because the function .get doesn't exist.
var modelBId = modelB.id; //This works because it is a simple object property access.
//...
As Chad Peruggia said, it seems that ExtJS creates special getters for reference fields that match the field name. Using getAssociatedData() returns only the primitive form of those objects (only their data values) but using the special getter (in my case getModelb()) it returns a full fledged model initialized with the given data.

Laravel Relations To a Pivot Table

In my Laravel app I have 3 Models ("Material", "Element" & "Reference") and their relation is like this:
"Material" is many to many to "Element"
"Each Material Element Relationship" is polymorphic many to many to "Reference"
Considering that I want to get all the info about these models in a single eager loading, what is the best practice to create the Models? Should I use a custom pivot model?
Update for better explanation:
I get the following result by using this code
Material::with('elements')->first();
What I'm looking for is the references for each pivot of ElementMaterial.
{
id: 1001,
name: "Butter",
elements: [
{
id: 203,
unit: "g",
name: "Protein",
pivot: {
material_id: 1001,
element_id: 203,
element_amount: 0.85
/*I NEED REFERENCES HERE!*/
},
},
{
id: 204,
unit: "g",
name: "FAT",
pivot: {
material_id: 1001,
element_id: 204,
element_amount: 81.11
/*I NEED REFERENCES HERE!*/
},
}
}
You definitely need custom pivot model for this. I assume you know how to create one, so here's just a suggestion on how to work with it:
// ElementMaterial = pivot model, elementMaterials = relation name
// OtherModel = model related to the above, otherModels = 1-m relation name
$material = Material::with('elements')->first();
// now to access otherModels you need something like this:
$material->elements->first()->pivot->otherModels;
Which will execute db query for each pivot model, overkill.
There is currently no way to eager load anything on the pivot model, even setting $with on the pivot itself won't help here.
You could instead do this:
$material = Material::with('elements', 'elementMaterials.otherModels')->first();
// but now you can access elements directly:
$material->elements; //collection of Element models
// and other models through the pivot model:
$material->elementMaterials; // collection of pivot models
$material->elementMaterials->first()->otherModels; // collection of OtherModels
But there is no link between $material->elements and $material->...->otherModels.
That's why I suggest you access both relations through the pivot model:
$material = Material::with('elementMaterials.elements', 'elementMaterials.otherModels')->first();
// then
$material->elementMaterials->first()->elements;
$material->elementMaterials->first()->otherModels;

Grid sorting with nested objects

How can I enable grid sorting with nested object. For example I have the following object which I bind locally to the grid
{
DataId: 1,
Duration: 4.5,
Customer: {
CustomerId: 1,
Name: 'Foo'
}
}
Now I want to enable sorting for the Name of the Customer (Customer.Name). The parse method didn't worked for me, cause I wrote a custom editortemplate which replace the complete customer with the new selected one.

Store with custom sorting in Sencha Touch

I have a store + model which is connected to a 3rd party plugin (Ext.ux.TouchGridPanel). The plugin calls the store's sort() method properly with the relevant mapping. Everything is working fine, and the store sorts itself. However, I would prefer to add customer sorting to the store. I have tried adding a sortType field into my model:
Ext.regModel("Transactions", {
fields: [
{
name: 'unit',
type: 'string',
sortType: function(value) {
console.log('PRINT GDAMNIT');
return 0;
}
},
...
]
});
This, however, is not working, and the sortType is not getting called.
TLDR: How to make custom sorting work for stores?
Your store will need a sorter added that will sort on that field before it will call the sortType function.
var store = new Ext.data.Store({
model: 'Transactions',
sorters: [
{
property: 'unit',
direction: 'DESC'
}
]}
);
Sort type converts the value of a field into another value to ensure proper ordering. If you aren't sorting on that field than there is no reason to call that function. You could add the sortDir to the field which would sort the field into ascending/descending order based on the type of the field alone.
A workaround might be to (I know this sounds inefficient but bear with me) add an extra field to your model instances (lets call it sortField) and use that for your sorting function. You can then loop through your model instances in your store applying your custom sorting algorithm and assign a sort value of 0,1,2,3,4,5 etc.. to sortField. Then in your store, you can add 'sorters: 'sortField'... Hope this helps a bit, I'm going through something similar at the current moment.
The custom SortType in Sencha Touch 2 works accordingly, as per http://docs.sencha.com/touch/2-0/#!/api/Ext.data.SortTypes:
Ext.apply(Ext.data.SortTypes, {
asPerson: function(person){
// expects an object with a first and last name property
return person.lastName.toUpperCase() + person.firstName.toLowerCase();
}
});
Ext.define('Employee', {
extend: 'Ext.data.Model',
config: {
fields: [{
name: 'person',
sortType: 'asPerson'
}, {
name: 'salary',
type: 'float' // sortType set to asFloat
}]
}
});
What you're attempting to do might be tricky. Calling store.sort() removes all existing sorters by default (according to Sencha Touch API documentation). To keep the existing sorters you would need to add the sorter to the MixedCollection store.sorters.
Secondly, to call the sort method with a custom sort function, you would need to pass a specific sorterFn instead of property to the Sorter (again, see the API for more details) - but this might prove tricky since the sort call is initiated from the plugin.
Not sure if this helps to solve your problem, but maybe it assists you to look at the right direction.

Resources