Dojo DataStore query - Using multiple parameters to search - dojo

I'm trying to query a dojo datastore object. The store has multiple objects, each one as a key titled OBJECTID with a unique value. Is there a way to query the store like this:
dataStoreObjet.query({OBJECTID : 6990, OBJECTID: 34277, OBJECTID: 9501 } );
Right now it only returns the last object. I'm sure I'm missing something simple, but it is driving me bonkers. I don't want to have to run a query for each value and then create a new set of values from all those queries if I don't have to.
Thanks

The object {OBJECTID : 6990, OBJECTID: 34277, OBJECTID: 9501 } will only contain one value that of the last one.
This is because a javascript object is a map of properties and its value.
The {OBJECTID : 6990, OBJECTID: 34277, OBJECTID: 9501 } definition is same as
var obj = { OBJECTID : 6990 };
obj.OBJECTID = 34277;
obj.OBJECTID = 9501;
As you can see from the above the obj object contains the last value i.e 9501. In effect your query to the dataStoreObjet
is
dataStoreObjet.query({OBJECTID : 9501} );
Now the solution to your problem.
You need to pass a function as a query parameter that will compare the values and return true or false for the objects to be part of the query result.
dataStoreObjet.query( function(object){
return (object.OBJECTID == 6990) || (object.OBJECTID == 6990) || (object.OBJECTID == 6990) ;
}) // Pass a function to do more complex querying

Related

How to filter list of objects by the value of the one field in Kotlin?

I have got list of objects
listOf(User("John",32), User("Katy",15), User("Sam",43))
How can write a function which returns me the User object if in parameter I pass a name. For example getUser("John") and it suppose to return me User("John",32)
One possibility is also using firstOrNull:
val list = listOf(User("John",32), User("Katy",15), User("Sam",43))
list.firstOrNull { it.name == "John" }

A variable is assigned with objects, how do I return the number of objects in the variable

I have an object assigned to a variable bio. I just want to return the number of objects assigned to bio (in this case 1).
var bio = {
"name" : "Dave Smith",
"role" : "Web developer",
};
I can find the number of key value pairs but I just want the number of objects.
New at this so not sure if this makes sense.
Is it even possible to have multiple objects in a variable?
any help appreciated.
You have two questions here (I assume you use javascript):
How to know how many objects are assigned to a variable?
You need to do .length on the object. Note that this will work only on something that can be enumerated e.g. array.
var a = {foo: 'bar'};
a.length // undefined
var b = [{foo: 'bar'}];
b.length // 1
How to store multiple objects inside a variable
You need to use arrays like so: var bio = [{name: 'foo'}, {name: 'bar'}];

Select from IndexedDB database, using multiple indexes

Each of my IndexedDB objects have the following format:
object:
{
objectName: "someName"
objectTest: "someTest"
objectAge: "someAge"
}
Then I have the following indexes already set:
storeObject.createIndex( "by_objectName", "objectName", { unique: false } );
storeObject.createIndex( "by_objectTest", "objectTest", { unique: false } );
storeObject.createIndex( "by_objectAge", "objectAge", { unique: false } );
So first I wanted to loop through all my objects by objectName (this is working):
var openedIndex = storeObject.index("by_objectName");
var numItemsInIndex = openedIndex.count();
if (openedIndex) {
var curCursor = openedIndex.openCursor();
curCursor.onsuccess = function(evt) {
var cursor = evt.target.result;
if (cursor) {
//do something
cursor.continue();
}
}
}
So the above code is taking all the objects and they are sorted by objectName.
How can I take all the objects that have objectTest: "certainValue" and the sorting by objectName to remain the same as in the above example. I need to filter the list of result before the line if (openedIndex) { because I need to use the numItemsInIndex later in the loop.
So in other words, if this was relational database, how to implement:
SELECT * FROM objects WHERE objectTest = "certainValue" SORT BY objectName
You can create an index on two properties at once by defining the keyPath parameter to createIndex as an array. Use the property you wish to sort by as the first item in the array. For example, see my other posts, like this one: In IndexedDB, is there a way to make a sorted compound query?

SQL to Magento model understanding

Understanding Magento Models by reference of SQL:
select * from user_devices where user_id = 1
select * from user_devices where device_id = 3
How could I perform the same using my magento models? getModel("module/userdevice")
Also, how can I find the number of rows for each query
Following questions have been answered in this thread.
How to perform a where clause ?
How to retrieve the size of the result set ?
How to retrieve the first item in the result set ?
How to paginate the result set ? (limit)
How to name the model ?
You are referring to Collections
Some references for you:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
http://alanstorm.com/magento_collections
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
lib/varien/data/collection/db.php and lib/varien/data/collection.php
So, assuming your module is set up correctly, you would use a collection to retrieve multiple objects of your model type.
Syntax for this is:
$yourCollection = Mage::getModel('module/userdevice')->getCollection()
Magento has provided some great features for developers to use with collections. So your example above is very simple to achieve:
$yourCollection = Mage::getModel('module/userdevice')->getCollection()
->addFieldToFilter('user_id', 1)
->addFieldToFilter('device_id', 3);
You can get the number of objects returned:
$yourCollection->count() or simply count($yourCollection)
EDIT
To answer the question posed in the comment: "what If I do not require a collection but rather just a particular object"
This depends if you still require both conditions in the original question to be satisfied or if you know the id of the object you wish to load.
If you know the id of the object then simply:
Mage::getModel('module/userdevice')->load($objectId);
but if you wish to still load based on the two attributes:
user_id = 1
device_id = 3
then you would still use a collection but simply return the first object (assuming that only one object could only ever satisfy both conditions).
For reuse, wrap this logic in a method and place in your model:
public function loadByUserDevice($userId, $deviceId)
{
$collection = $this->getResourceCollection()
->addFieldToFilter('user_id', $userId)
->addFieldToFilter('device_id', $deviceId)
->setCurPage(1)
->setPageSize(1)
;
foreach ($collection as $obj) {
return $obj;
}
return false;
}
You would call this as follows:
$userId = 1;
$deviceId = 3;
Mage::getModel('module/userdevice')->loadByUserDevice($userId, $deviceId);
NOTE:
You could shorten the loadByUserDevice to the following, though you would not get the benefit of the false return value should no object be found:
public function loadByUserDevice($userId, $deviceId)
{
$collection = $this->getResourceCollection()
->addFieldToFilter('user_id', $userId)
->addFieldToFilter('device_id', $deviceId)
;
return $collection->getFirstItem();
}

Conditionally adjust visible columns in Rally Cardboard UI

So I want to allow the user to conditionally turn columns on/off in a Cardboard app I built. I have two problems.
I tried using the 'columns' attribute in the config but I can't seem to find a default value for it that would allow ALL columns to display(All check boxes checked) based on the attribute, ie. the default behavior if I don't include 'columns' in the config object at all (tried null, [] but that displays NO columns).
So that gets to my second problem, if there is no default value is there a simple way to only change that value in the config object or do I have to encapsulate the entire variable in 'if-else' statements?
Finally if I have to manually build the string I need to parse the values of an existing custom attribute (a drop list) we have on the portfolio object. I can't seem to get the rally.forEach loop syntax right. Does someone have a simple example?
Thanks
Dax - Autodesk
I found a example in the online SDK from Rally that I could modify to answer the second part (This assumes a custom attribute on Portfolio item called "ADSK Kanban State" and will output values to console) :
var showAttributeValues = function(results) {
for (var property in results) {
for (var i=0 ; i < results[property].length ; i++) {
console.log("Attribute Value : " + results[property][i]);
}
}
};
var queryConfig = [];
queryConfig[0] = {
type: 'Portfolio Item',
key : 'eKanbanState',
attribute: 'ADSK Kanban State'
};
rallyDataSource.findAll(queryConfig, showAttributeValues);
rally.forEach loops over each key in the first argument and will execute the function passed as the second argument each time.
It will work with either objects or arrays.
For an array:
var array = [1];
rally.forEach(array, function(value, i) {
//value = 1
//i = 0
});
For an object:
var obj = {
foo: 'bar'
};
rally.forEach(obj, function(value, key) {
//value = 'bar'
//key = 'foo'
});
I think that the code to dynamically build a config using the "results" collection created by your query above and passed to your sample showAttributeValues callback, is going to look a lot like the example of dynamically building a set of Table columns as shown in:
Rally App SDK: Is there a way to have variable columns for table?
I'm envisioning something like the following:
// Dynamically build column config array for cardboard config
var columnsArray = new Array();
for (var property in results) {
for (var i=0 ; i < results[property].length ; i++) {
columnsArray.push("'" + results[property][i] + "'");
}
}
var cardboardConfig = {
{
attribute: 'eKanbanState',
columns: columnsArray,
// .. rest of config here
}
// .. (re)-construct cardboard...
Sounds like you're building a neat board. You'll have to provide the board with the list of columns to show each time (destroying the old board and creating a new one).
Example config:
{
attribute: 'ScheduleState'
columns: [
'In-Progress',
'Completed'
]
}