Can't get the elements of the sencha store - sencha-touch

So I have a store
Ext.define('APN.store.BackupShow', {
extend: 'Ext.data.Store',
requires: [
'APN.model.ScheduledShow'
],
config: {
model: 'APN.model.ScheduledShow',
proxy: {
type: 'ajax',
url: '',
reader: {
type: 'xml',
record: 'item',
rootProperty: 'xml'
}
}
},
getShow: function () {
if (this.getData().length greater 1) # by some reason stackoverflow didn't allow me to put greater sign in here;
return null;
// Copy field data across as wrong field is popped.
this.getAt(0).set("listimage", this.getAt(0).get("onairimage"));
this.getAt(0).set("isbackup", "true");
return this.getAt(0);
}
});
And when I'm trying to call the first element of the store I get undefined, however the element exists in the store:
(0) console.log(backupShowStore);
(1) console.log(backupShowStore.data);
(2) console.log(backupShowStore.getData().all);
(3) console.log(backupShowStore.getData().all.length);
(4) console.log(backupShowStore.getData().all.getAt(0));
I got back:
(1)
Class
_data: Class
_model: function () {
_modelDefaults: objectClass
_proxy: Class
_remoteFilter: false
_remoteSort: false
_storeId: "backupShow"
_totalCount: null
config: objectClass
data: Class
_autoFilter: true
_autoSort: true
_filterRoot: "data"
_sortRoot: "data"
all: Array[1]
0: Class
_data: Object
data: Object
bufferingProgress: null
contentlink: null
description: null
facebooklink: "http://www.facebook.com/mixmelbourne"
id: "ext-record-45"
isbackup: null
listimage: null
onairimage: "http://arntrnassets.mediaspanonline.com/radio/mxm/53808/on-air-default_v3.png"
showbody: "Melbourne's widest variety from 2K to today, Mix101.1 with Chrissie & Jane waking up Melbourne weekdays from 6am."
showbyline: "The widest variety from 2K to today"
showcontentxml: null
showemail: null
showname: "Mix 101.1"
showschedule: null
smallimage: null
title: null
twittername: "mixmelbourne"
__proto__: Object
id: "ext-record-45"
internalId: "ext-record-45"
modified: Object
phantom: true
raw: item
stores: Array[1]
__proto__: TemplateClass
length: 1
__proto__: Array[0]
(2)
_autoFilter: true
_autoSort: true
_filterRoot: "data"
_sortRoot: "data"
all: Array[1]
config: objectClass
dirtyIndices: true
getKey: function (record) {
indices: Object
initConfig: function (){}
initialConfig: Object
items: Array[1]
keys: Array[1]
length: 1
map: Object
__proto__: TemplateClass
(3)
Array[1]
0: Class
length: 1
__proto__: Array[0]
(4)
0
(5)
Uncaught TypeError: Object [object Array] has no method 'getAt'
Which is understandable for (5) as array doesn't have method getAt, however the store doesn't have any items and that is indicated by (4) where the array of getData elements equals to 0...
Am very confused at this point of time with Sencha Touch Framework and how to get the first element of an array of elements

Why not just use the Ext.data.Store.first() method.
I have found I am typically a happier developer when I use the methods provided by the api. On the rare occasion that I need something not provided I will navigate the Sencha Objects myself but I really try not to.
Let me know if and why that solution might not work and I'll try to find something else for you.

Related

How can I retrieve nested values in Keystone 5 for my list

I'm adding a list called 'tourlocation' to my Keystone 5 project. In my mongo database my tourlocations collection has an object called 'coordinates', with two values: 'lat' and 'long'. Example:
"coordinates" : {
"lat" : 53.343761,
"long" : -6.24953
},
In the previous version of keystone, I could define my tourlocation list coordinates object like this:
coordinates: {
lat: {
type: Number,
noedit: true
},
long: {
type: Number,
noedit: true
}
Now unfortunately, when I try to define the list this way it gives the error: The 'tourlocation.coordinates' field doesn't specify a valid type. (tourlocation.coordinates.type is undefined)'
Is there any way to represent objects in keystone 5?
#Alex Hughes I believe your error says "type" which you may need to add it like this
keystone.createList('User', {
fields: {
name: { type: Text }, // Look at the type "Text" even in the MongoDB you can choose the type but it will be better to choose it here from the beginning.
email: { type: Text },
},
});
Note that noedit: true is not supported in version 5 of KeystoneJS.
For more info look at this page https://www.keystonejs.com/blog/field-types#core-field-types

RoR model.where() using pg array type

I stumbled on the idea of limiting my tables and associations by using arrays in my models like so.
I'm working on a task assignment app where the user will, in essence, construct a sentence to perform an action. I'm using keywords to help structure the boundaries of the sentence.
Examples include (keywords in brackets):
"[I will] paint the fence" <- makes a new task, assigned to current_user
"[Ask] Huck [to] paint the fence" <- find_or_create task, assign to find_or_create user
"[Archive] painting the fence" <- soft-delete task
So here's my migration:
class CreateKeywords < ActiveRecord::Migration[5.1]
def change
create_table :keywords do |t|
t.string :name, null: false
t.text :pre, array: true, default: []
t.text :post, array: true, default: []
t.string :method, null: false, default: "read" # a CRUD indicator
end
end
end
keyword.post indicates what models could follow the keyword
keyword.pre indicates what models could preceed the keyword
My seed data looks like this:
Keyword.create([
{ name: "i will", post: ["task"] },
{ name: "ask", post: ["user"] },
{ name: "assign", post: ["user", "task"] },
{ name: "find a", post: ["user", "task"] },
{ name: "make a new", post: ["user", "task"], method: "create" },
{ name: "finalize", post: ["task"] },
{ name: "archive", post: ["user", "task"], method: "delete" },
{ name: "update", post: ["user", "task"], method: "update" },
{ name: "for", post: ["user", "task"], pre: ["user", "task"] },
{ name: "to", post: ["user", "task"], pre: ["user", "task"] },
{ name: "and repeat", pre: ["task"] },
{ name: "before", pre: ["task"] },
{ name: "after", pre: ["task"] },
{ name: "on", pre: ["task"] }
])
Now I want to do things like:
key = Keyword.third
Keyword.where(pre: key.post)
But this returns exact matches and I want to do something like:
"Return all keywords where any value of key.post can be found in Keyword.pre"
I haven't had any luck along these lines:
Keyword.where(pre.include? key.post)
I can iterate over all the Keywords and use AND:
results = []
Keyword.all.each do |k|
comb = k.pre & key.post
if comb.present?
results << k
end
end
results.map { |k| k.name }
But this feels bad.
And I'm a bit out of my depth on the SQL necessary to do this.
Any pointers?
There are two things you want to know about:
PostgreSQL's "array constructor" syntax which looks like array['a', 'b', 'c'] rather than the more common '{a,b,c}' syntax.
PostgreSQL's array operators.
Array constructor syntax is convenient because when ActiveRecord sees an array as the value for a placeholder, it expands the array as a comma delimited list which works equally well with x in (?) and x && array[?].
For the operator to use, you want:
all keywords where any value of key.post can be found in Keyword.pre
which is another way of saying that key.post and Keyword.pre overlap and the operator for that is &&. There are also subset (<#) and superset (#>) operators if you need slightly different logic.
Putting that together:
Keyword.where('pre && array[?]', key.post)
or:
Keyword.where('pre && array[:post]', post: key.post)
lacostenycoder, in the comments, is right to be concerned empty arrays. ActiveRecord expands empty arrays to a single null when filling in a placeholder so you could end up doing SQL like:
pre && array[null]
and PostgreSQL won't be able to determine the type of array[null]. You can include a cast:
Keyword.where('pre && array[:post]::text[]', post: key.post)
# --------------------------------^^^^^^^^ tell PostgreSQL that this is an array of text
But, since pre && array[null]::text[] will never be true, you could just skip the whole thing if key.post.empty?.
Empty arrays don't overlap with any other array (not even another empty array) so you don't need to worry about empty arrays beyond what ActiveRecord will do with them. Similarly for pre is null, null && any_array will never be true (it will actually evaluate to null) so there won't be any overlaps with such things.
This is actually much easier that you would expect:
Keyword.where("pre && post")
# testing vs your seed data plus a few I added returns:
#Keyword Load (1.0ms) SELECT "keywords".* FROM "keywords" WHERE (pre && post)
#[<Keyword:0x007fc03c0438b0 id: 9, name: "for", pre: ["user", "task"], post: ["user", "task"], method: "read">,
#<Keyword:0x007fc03c0435b8 id: 10, name: "to", pre: ["user", "task"], post: ["user", "task"], method: "read">,
#<Keyword:0x007fc03c043248 id: 15, name: "foo", pre: ["bar", "baz"], post: ["baz", "boo"], method: "read">,
#<Keyword:0x007fc03c042f28 id: 16, name: "rock", pre: ["anthem"], post: ["ballad", "anthem"], method: "read">,
#<Keyword:0x007fc03c042cf8 id: 17, name: "disco", pre: ["mood"], post: ["ballad", "anthem", "mood"], method: "read">]

How to define an example request body containing an array of complex objects in Swagger?

I am trying to write the Swagger spec for a service that posts an array of objects as request body. I would like the user to be able to test the service with a specific set of multiple different complex objects in the array as the default sample inputs.
So far I have the following code defining the service and complex object:
paths:
/myService
post:
summary: test 123
description: test 123
parameters:
- name: bodyParamsObject
description: 'Object containing any / all params in the body'
in: body
required: true
schema:
properties:
data:
$ref: '#/definitions/myInputArray'
responses:
200:
description: OK
schema: myOutputArray
definitions:
myInputArray:
type: array
items:
$ref: '#/definitions/myComplexObject'
myOutputArray:
type: array
items:
$ref: '#/definitions/myComplexObject'
myComplexObject:
type: object
properties:
description:
type: string
example: 'Example Item'
size:
example: 552
type: integer
format: int32
hasAdditionalProperties:
type: boolean
example: true
The output array is coming back correctly, but there is only one item in the model schema.
How can I make the sample request body contain multiple different items in the array?
I was able to solve this by using the example property on the object definition and filling it with the array in json.
definitions:
myInputArray:
type: array
items:
$ref: '#/definitions/myComplexObject'
example: [
{
"description": "Example Item 1",
"hasAdditionalProperties": true,
"size": 750,
},
{
"description": "Another example",
"hasAdditionalProperties": false,
"size": -22,
},
{
"description": "Last one",
"hasAdditionalProperties": true,
"size": 0,
}
]
myComplexObject:
type: object
properties:
description:
type: string
example: 'Example Item'
size:
example: 552
type: integer
format: int32
hasAdditionalProperties:
type: boolean
example: true

How to search array of int as property?

My Show Object:
class Show extends Realm.Object { }
Show.schema = {
name: 'Show',
primaryKey: 'showId',
properties: {
showId: 'int',
showName:{ type: 'string', default: '' },
episodes:{ type: 'int[]', default: [] },
}
};
How then can I search the Show objects via the episodes property?
I have already search here and there and tried:
.filtered('episodes == $0',12345)
.filtered('episodes IN $0',12345)
but nothing works.
IN predicates aren't yet supported, however, there are a couple of workarounds:
Property Array Contains Value
If you want to know if an array property on your Realm object contains one or more values, you can't use primitive values like int[] in your example. If instead you create an Episode schema with an id property, you can then do a filtered('episodes.id == $0', 12345).
Scalar Property in Array
This can be done by mapping and joining a set of predicates until IN is supported which looks something like the following psuedocode:
.filterted([1,2,3].map(id => 'property == id').join(' OR '))

Sencha Touch Cannot call method 'substr' of null on Loacations:10

Hi as the Title says i am getting this error suddenly without changing anything.
This is the File Locations Code:
Ext.define('Wickelplaetze.store.Locations', {
extend: 'Ext.data.Store',
requires: 'Wickelplaetze.model.Location',
config: {
model: 'Wickelplaetze.model.Location',
storeId: 'locationsstore',
grouper: {
groupFn: function(record) {
return record.get('ort').substr(0, 1);
},
sortProperty: 'ort'
},
proxy: {
type: 'ajax',
url: 'http://freakp.com/wpapp/form-data.json',
withCredentials: false,
useDefaultXhrHeader: false
},
autoLoad: true
}
});
There are null values in you json for key ort. You can check if ort is not null and then return like -
if(record.get("ort")!= null){
return record.get('ort')[0];
}
Will remove that error doing so. But this will not sort records properly.
One more thing, if you want to sort list by first letter of ort , you can directly use -
return record.get("ort")[0];
When I tried your code to populate list, its actually running infinitely. I didn't get anything. Sorting these much values is damn slow. It took 3 mins to populate the list.
UPDATE
Link for working fiddle for your example. You can see null values at bottom of list. There are 7 null values for key ort.