I have the following store:
Ext.define('Sencha.model.MenuPoint', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'id', type: 'string'},
// this is id of the element, example child id, report id, and so fourth
//{name: 'elementId', type: 'int'},
{name: 'name', type: 'string'},
{name: 'icon_url', type: 'string'},
{name: 'xtype', type: 'string'}
]
}
});
And I manage to insert and retrieve the value like this:
var keyValue = new Object();
keyValue.key = "adultId";
keyValue.value = adultObj.adultId;
console.log("keyValue: "+keyValue);
var sessionStore = Ext.getStore('MySessionStore');
sessionStore.add(keyValue);
sessionStore.sync();
var index = sessionStore.find('key',keyValue.key);
console.log("index: "+index);
var keyValue2 = sessionStore.getAt(index);
console.log("keyValue2: "+keyValue2);
But it so cumbersome to first get the index, then get the value, isnt it possible to do something like this:
var value = store.get(key);
?
you could use function
var record = store.findRecord('id', key)
it is a shortcut to
index = store.find('id', key);
record = index != -1 ? store.getAt(index) : null;
Cheers, Oleg
Use
store.findRecord('id', key, 0, false, true, true);
By default, findRecord search uses regexp. You should pass all 6 parameters to get expresstion like ===
Or if you find by "id" try store.getById(key)
See fiddle
Related
I had a code like below
const changes = dto.data.map(d => `('${d.findingId}', '${d.status}', ${d.comment ? `'${d.comment}'` : null})`).join(',')
const query = `
MERGE \`${projectId}.dlp.result-status\`
USING UNNEST([struct<findingId STRING, status STRING, comment STRING> ${changes} ]) changes
ON \`${projectId}.dlp.result-status\`.findingId = changes.findingId
WHEN NOT MATCHED THEN
INSERT (findingId, status, modifiedAt, comment ) VALUES (changes.findingId, changes.status, '${now}', changes.comment)
WHEN MATCHED THEN
UPDATE SET
\`${projectId}.dlp.result-status\`.status = changes.status,
\`${projectId}.dlp.result-status\`.comment = changes.comment
;
`;
const options = {
query: query,
// Location must match that of the dataset(s) referenced in the query.
location: 'US'
};
but it was vulnerable to SQL injection, so I changed my code to use parameters
const changes = dto.data.map(d => ({ findingId: d.findingId, status: d.status, comment: d.comment }))
const query = `
MERGE \`${projectId}.dlp.result-status\`
USING UNNEST(#changes) changes
ON \`${projectId}.dlp.result-status\`.findingId = changes.findingId
WHEN NOT MATCHED THEN
INSERT (findingId, status, modifiedAt, comment ) VALUES (changes.findingId, changes.status, #now, changes.comment)
WHEN MATCHED THEN
UPDATE SET
\`${projectId}.dlp.result-status\`.status = changes.status,
\`${projectId}.dlp.result-status\`.comment = changes.comment
;
`;
const options: Query = {
query: query,
// Location must match that of the dataset(s) referenced in the query.
location: 'US',
params: { now: now, changes: changes },
types: {
now: 'STRING',
// changes: [{ 'findingId': 'STRING', 'status': 'STRING', 'comment': 'STRING' }]
// changes: ['STRUCT']
}
};
so important change is changing changes variable from concatenated string to array of objects and changing USING UNNEST([struct<findingId STRING, status STRING, comment STRING> ${changes} ]) changes to USING UNNEST(#changes) changes
When I ran this, I get this error
Parameter types must be provided for null values via the 'types' field in query options.
So I tried to add types, now I am stuck in how to add type for Array of struct, I tried these but no lock
changes: [{ 'findingId': 'STRING', 'status': 'STRING', 'comment': 'STRING' }]
changes: ['STRUCT']
ps.
This is the type definition on Query
Seems this is only typing (ts) issue, I made it like this and worked (added as any)
const options: Query = {
query: query,query.
location: 'US',
params: { now: now, changes: changes },
types: {
now: 'STRING',
changes: [{ findingId: 'STRING', status: 'STRING', comment: 'STRING' }] as any
}
};
I'm trying to create a custom grid with columns for Feature Predecessors and Successors.
I've managed to extract the data and display the relevant formatted IDs of the pre+suc in clear text. Now I would like to format these as "standard" FormattedIDs with QDP/click options.
My display looks like this, is this the right path, what should I return in order to have correct formatting?
var myGrid = Ext.create('Ext.Container', {
items: [
{
xtype: 'rallygrid',
columnCfgs: [
'FormattedID',
'Name',
{ // Column 'Successors'
xtype: 'templatecolumn',
tpl: Ext.create('Rally.ui.renderer.template.FormattedIDTemplate'),
dataIndex: 'Successors',
renderer: function(value, metaData, record) {
//console.log('Display in renderer: ', record.successorStore);
var mFieldOutputSuc = '';
var i;
var mDependency;
for (i = 0; i < record.successorStore.getCount(); i++) {
mDependency = record.successorStore.getAt(i);
console.log('mDependency = ', mDependency);
mFieldOutputSuc = mFieldOutputSuc + mDependency.get('FormattedID') + '<br>'; // Correct return value?
}
return mFieldOutputSuc;
}, //renderer: function(value, metaData, record) {
}, // Column 'Successors'
I'd check out this utility method: https://help.rallydev.com/apps/2.1/doc/#!/api/Rally.nav.DetailLink-method-getLink
You should be able to just put that in there where you have your //Correct return value? comment:
mFieldOutputSuc += Rally.nav.DetailLink.getLink({
record: mDependency
});
There are some more config options you can pass as well, to customize the link further, but I think that should get you started...
I need to have the name field of a model be virtual, created by concatenating two real fields together. This name is just for display only. I've tried the virtual examples in the doc, no luck. Keystone 4 beta5.
var keystone = require('keystone')
_ = require('underscore');
var Types = keystone.Field.Types;
/**
* Foo Model
* ==================
*/
var Foo = new keystone.List('Foo', {
map: {name: 'fooname'},
track: true
});
Foo.add({
bar: { type: Types.Relationship, required: true, initial: true, label: 'Barref', ref: 'Bar', many: false },
order: { type: Types.Select, required: true, initial: true, label: 'Order', options: _.range(1,100) },
price: { type: Types.Money, format: '$0,0.00', label: 'Price', required: true, initial: true },
});
Foo.schema.virtual('fooname').get(function() {
return this.bar+ ' ' + this.order;
});
Foo.defaultColumns = 'fooname, bar, order, price';
Foo.register();
When I use this model definition, I don't see the virtual name in the defaultcolumns list. I want to make a virtual name so lookups are easier when this model is used as a relationship.
You don't need a virtual to do this. Keystone allows you to track and recalculate a field every time the document is saved. You can enable those options in order to create a function which concatenates these two values for you (either synchronously or asynchronously, your choice.)
One other thing I noticed is that bar is a Relationship, which means you will need to populate that relationship prior to getting any useful information out of it. That also means your value function will have to be asynchronous, which is as simple as passing a callback function as an argument to that function. Keystone does the rest. If you don't need any information from this bar, and you only need the _id (which the model always has), you can do without the keystone.list('Bar') function that I included.
http://keystonejs.com/docs/database/#fields-watching
The map object also refers to an option on your model, so you'll need a fooname attribute on your model in any scenario, though it gets calculated dynamically.
var keystone = require('keystone'),
_ = require('underscore');
var Types = keystone.Field.Types;
/**
* Foo Model
* ==================
*/
var Foo = new keystone.List('Foo', {
map: {name: 'fooname'},
track: true
});
Foo.add({
fooname: { type: Types.Text, watch: true, value: function (cb) {
// Use this if the "bar" that this document refers to has some information that is relevant to the naming of this document.
keystone.list('Bar').model.findOne({_id: this.bar.toString()}).exec(function (err, result) {
if (!err && result) {
// Result now has all the information of the current "bar"
// If you just need the _id of the "bar", and don't need any information from it, uncomment the code underneath the closure of the "keystone.list('Bar')" function.
return cb(this.bar.name + " " + this.order);
}
});
// Use this if you don't need anything out of the "bar" that this document refers to, just its _id.
// return cb(this.bar.toString() + " " + this.order);
} },
bar: { type: Types.Relationship, required: true, initial: true, label: 'Barref', ref: 'Bar', many: false },
order: { type: Types.Select, required: true, initial: true, label: 'Order', options: _.range(1,100) },
price: { type: Types.Money, format: '$0,0.00', label: 'Price', required: true, initial: true },
});
Foo.defaultColumns = 'fooname, bar, order, price';
Foo.register();
try this:
Foo.schema.pre('save', function (next) {
this.name = this.bar+ ' '+ this.order;
next();
});
Could you provide more information? What is currently working? How should it work?
Sample Code?
EDIT:
After creating the model Foo, you can access the Mongoose schema using the attribute Foo.schema. (Keystone Concepts)
This schema provides a pre-hook for all methods, which registered hooks. (Mongoose API Schema#pre)
One of those methods is save, which can be used like this:
Foo.schema.pre('save', function(next){
console.log('pre-save');
next();
});
I'm using Dojo GridX with many modules, including filter:
grid = new Grid({
cacheClass : Cache,
structure: structure,
store: store,
modules : [ Sort, ColumnResizer, Pagination, PaginationBar, CellWidget, GridEdit,
Filter, FilterBar, QuickFilter, HiddenColumns, HScroller ],
autoHeight : true, autoWidth: false,
paginationBarSizes: [25, 50, 100],
paginationBarPosition: 'top,bottom',
}, gridNode);
grid.filterBar.applyFilter({type: 'all', conditions: [
{colId: 'type', condition: 'equal', type: 'Text', value: 'car'}
]})
I've wanted to access the items, that are matching the filter that was set. I've travelled through grid property in DOM explorer, I've found many store references in many modules, but all of them contained all items.
Is it possible to find out what items are visible in grid because they are matching filter, or at least those that are visible on current page? If so, how to do that?
My solution is:
try {
var filterData = [];
var ids = grid.model._exts.clientFilter._ids;
for ( var i = 0; i < ids.length; ++i) {
var id = ids[i];
var item = grid.model.store.get(id);
filterData.push(item);
}
var store = new MemoryStore({
data : filterData
});
} catch (error) {
console.log("Filter is not set.");
}
I was able to obtain filtered gridX data rows using gridX Exporter. Add this Exporter module to your grid. This module does exports the filtered data. Then, convert CSV to Json. There are many CSV to Json conversion javasripts out there.
this.navResult.grid.exporter.toCSV(args).then(this.showResult, this.onError, null)
Based on AirG answer I have designed the following solution. Take into account that there are two cases, with or without filter and that you must be aware of the order of rows if you have applied some sort. At least this works for me.
var store = new Store({
idProperty: "idPeople", data: [
{ idPeople: 1, name: 'John', score: 130, city: 'New York', birthday: '31/02/1980' },
{ idPeople: 2, name: 'Alice', score: 123, city: 'Wáshington', birthday: '07/12/1984' },
{ idPeople: 3, name: 'Lee', score: 149, city: 'Shanghai', birthday: '8/10/1986' },
...
]
});
gridx = new GridX({
id: 'mygridx',
cacheClass: Cache,
store: store,
...
modules: [
...
{
moduleClass: Dod,
defaultShow: false,
useAnimation: true,
showExpando: true,
detailProvider: gridXDetailProvider
},
...
],
...
}, 'gridNode');
function gridXDetailProvider (grid, rowId, detailNode, rendered) {
gridXGetDetailContent(grid, rowId, detailNode);
rendered.callback();
return rendered;
}
function gridXGetDetailContent(grid, rowId, detailNode) {
if (grid.model._exts.clientFilter._ids === undefined || grid.model._exts.clientFilter._ids === 0) {
// No filter, with or without sort
detailNode.innerHTML = 'Hello ' + grid.row(grid.model._cache._priority.indexOf(rowId)).item().name + " with id " +
grid.row(grid.model._cache._priority.indexOf(rowId)).item().idPeople;
} else {
// With filter, with or without sort
detailNode.innerHTML = 'Hello ' + grid.row(grid.model._exts.clientFilter._ids.indexOf(rowId)).item().name + " with id " +
grid.row(grid.model._exts.clientFilter._ids.indexOf(rowId)).item().idPeople;
}
}
Hope that helps,
Santiago Horcajo
function getFilteredData() {
var filteredIds = grid.model._exts.clientFilter._ids;
return grid.store.data.filter(function(item) {
return filteredIds.indexOf(item.id) > -1;
});
}
I am working in sencha touch 1.1, specifically, using Ext.List. But instead of using a proxy to populate the Ext.List object, I need to load the data right straight from an array javascript variable like
var = [["1","Ángel Quezada"],["2","Christian Herrera"],["3","Francisco Quispe"]]
I don't have any trouble populating the Ext.List in the proxy way, first I declare le model, then the proxy and finally the List. According the next lines.
Ext.regModel('Cronograma', {
idProperty: 'nrocuota',
fields: [{name:'nrocuota', type: 'string'}]
});
Ext.regStore('CronogramaStore', {
model: 'Cronograma',
proxy: {
type :'ajax',
url: '/mSAS/cotizacion.do?method=generarCronograma',
reader: {type:'array'}
}
});
this.grdCronograma = new Ext.List({
id: 'notesList',
store: 'CronogramaStore',
emptyText: 'No existe resultado ',
itemTpl: '{nrocuota}',
listeners: {'render':function(thisComponent){}}
});
But right know I have another need. I need to populate the List from an Array Javascript Variable, not using the proxy, I mean
how can I use the
var varArray = [["1","Ángel Quezada"],["2","Christian Herrera"],["3","Francisco Quispe"]]
to populate the Ext.List, I guess there's a way using the Ext.data.Store and its load method. but I can't find it.
Here is dynamic way of doing it, without using static data.
Ext.regModel('Cronograma', {
idProperty: 'nrocuota',
fields: [{name:'nrocuota', type: 'string'}]
});
Ext.regStore('CronogramaStore', {
model: 'Cronograma'/*,
data : [
{ nrocuota : 'Ángel Quezada' },
{ nrocuota : 'Christian Herrera' },
{ nrocuota : 'Francisco Quispe' }] */ // Does not take array [[],[],[]] that was specified in problem statement
});
this.grdCronograma = new Ext.List({
id: 'notesList',
store: 'CronogramaStore',
emptyText: 'No existe resultado ',
itemTpl: '{nrocuota}',
listeners: {'render':function(thisComponent){}}
});
// Here is the dynamic method.
function addToList(data){
var len = data.length;
for(var i=0;i<len;i++){
var note = Ext.ModelMgr.create({
nrocuota: data[i][1], //grab only name from the array
}, 'Cronograma');
CronogramaStore.add(note);
CronogramaStore.sync();
}
}
//now just call the function
var data = [["1","Ángel Quezada"],["2","Christian Herrera"],["3","Francisco Quispe"]];
addToList(data);
Ext.regModel('Cronograma', {
idProperty: 'nrocuota',
fields: [{name:'nrocuota', type: 'string'}]
});
Ext.regStore('CronogramaStore', {
model: 'Cronograma',
data : [
{ nrocuota : 'Ángel Quezada' },
{ nrocuota : 'Christian Herrera' },
{ nrocuota : 'Francisco Quispe' }
]
});
this.grdCronograma = new Ext.List({
id: 'notesList',
store: 'CronogramaStore',
emptyText: 'No existe resultado ',
itemTpl: '{nrocuota}',
listeners: {'render':function(thisComponent){}}
});
update to use arrays instead of json object:
Ext.regModel('Cronograma', {
idProperty: 'id',
fields: [ 'id', 'nrocuota' ]
});
var data = [["1","Ángel Quezada"],["2","Christian Herrera"],["3","Francisco Quispe"]];
Ext.regStore('CronogramaStore', {
model: 'Cronograma',
data : data
});
this.grdCronograma = new Ext.List({
id: 'notesList',
store: 'CronogramaStore',
emptyText: 'No existe resultado ',
itemTpl: '{nrocuota}',
listeners: {'render':function(thisComponent){}}
});
or already indicated if you don't have data available at time of creation of store then use store.loadData(data); function
i haven't tested this code myself so if it's not working then please tell me