Accessing data from a Store in ExtJS? - javascript

So I have the following code:
The Model:
Ext.define('Sandbox.model.User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Name', type: 'string' },
{ name: 'Email', type: 'string' },
{ name: 'TelNumber', type: 'string' },
{ name: 'Role', type: 'string' }
]
});
The Store
Ext.define("Sandbox.store.Users", {
extend : 'Ext.data.Store',
model : 'Sandbox.model.User'
});
And I have the following Application.js which loads the data onto to the Store:
Ext.define('Sandbox.Application', {
extend: 'Ext.app.Application',
name: 'Sandbox',
stores: [
'Users'
],
launch: function () {
var userStore = Ext.getStore('Users');
userStore.add(
[
{
Name : 'John',
Email: 'john#gmail.com',
TelNumber : ' 0330 122 2800',
Role : 'Administrator'
},
{
Name : 'Sarah',
Email : 'sarah#hotmail.com',
TelNumber: ' 0330 122 2800',
Role : 'Customer'
},
{
Name : 'Brian',
Email : 'brian#aol.com',
TelNumber: ' 0330 122 2800',
Role : 'Supplier'
},
{
Name : 'Karen',
Email : 'karen#gmail.com',
TelNumber: ' 0330 122 2800',
Role : 'Administrator'
}
]
);
var userStore = Ext.getStore('Users');
console.log("Store size " + userStore.getCount());
console.log("Person #1: " + userStore.getAt(0).Name);
}
});
When the application is started, the correct store size(4) is logged. But the second log for a specific data at index 0 returns the value undefined. I understand that ExtJS is asynchronus but the data is hard coded; why would there be a delay in loading the data into the store ? How can I fix this code so that when I first navigate to the application on the browser, the above two logs display the correct information ?

You cannot directly access to the property. You should use:
userStore.getAt(0).get("Name")
EDIT: There is nothing else wrong in your code, the problem is just the syntax. "ExtJS is asynchronus" is a big misunderstanding as it is just a js library, it can't be async. The way you code it can be, and you dont in your example. You manually add data to your store.

Related

JSON logic Checking best approach

I have a search form and more than 15 fields for searching. I have to call different API's based on the search Parameters
For instance below is my request Params
requestParams = {
firstName : 'Jack',
lastName : 'Jill',
dob : '12-10-2020',
email : '',
phone : '',
address: '',
postCode: '',
customerID : '11111111',
status : 'Active'
}
Currently am using if conditions to achieve this
if( checkNull(requestParams.firstName) && checkNull(requestParams.lastName) ) {
callApiOne();
}
if( checkNull(requestParams.customerID)) {
callApiTwo();
}
if( checkNull(requestParams.firstName) && checkNull(requestParams.status) ) {
callApiOne();
}
if( checkNull(requestParams.firstName) && checkNull(requestParams.phone) ) {
callApiTwo();
}
My search combination list may increase up to 25 what is the best approach to keep it configurable and reusable.
Based on the input you have given. I came up with the solution.
Given your request param
const requestParams = {
firstName : 'Jack',
lastName : 'Jill',
dob : '12-10-2020',
email : '',
phone : '',
address: '',
postCode: '',
customerID : '11111111',
status : 'Active',
};
First create a variable containing all the fields you need which contains the key of your request param and the api based on that.
const PARAMS = [
{ key: 'lastName', api: 'apiOne' },
{ key: 'dob', api: 'apiTwo' },
{ key: 'email', api: 'apiThree' },
{ key: 'phone', api: 'apiFour' },
{ key: 'address', api: 'apiFive' },
{ key: 'postCode', api: 'apiSix' },
{ key: 'customerID', api: 'apiSeven' },
{ key: 'status', api: 'apiOne' },
];
Then loop through each items from above list and check if requestParams contains a value and if its not null. For some like customerID you can have a exception.
PARAMS.forEach(({ key, api }) => {
if (key === 'customerID' && checkNull(requestParams.customerID)) {
callApi(api);
return;
}
if (checkNull(requestParams.firstName) && checkNull(requestParams[key])) {
callApi(api);
}
});
If you post your full code I can help you better.

Access to table user in sequelize

How can I get the information of 2 users when I request my friends table?
My table friends is formatted like this:
id X , UserId : 1 , idFriend : 3, ...
My table User is formatted like this:
id X , name , mail , ...
When I link model user in my query, I get only the information of UserID, but I don't get the information on idFriend
My request is
models.Friend.findAll({
where: {
$or: [{
idFriend: userFound.id
},
{
UserID: userFound.id
}],
status: "pending"
},
include: [
{ model: models.User },
]
})
How can I do that ?
1st Define association like this in your friend model :
Friend.belongsTo(User, { foreignKey : 'UserID', constraints: false});
Friend.belongsTo(User, { as: 'friend', foreignKey : 'idFriend' , constraints:false});
2nd thing is include model like this :
models.Friend.findAll({
where: {
$or: [{
idFriend: userFound.id
},
{
UserID: userFound.id
}],
status: "pending"
},
include: [
{
model: models.User ,
required : false,
where : { id : userFound.id }
},{
model: models.User ,
as : 'friend', // <--------- Magic is here
required : false,
where : { id : userFound.id }
}
]
})

Error in SailsJs. TypeError: Cannot read property 'attributes' of undefined

I'm working with sailsjs and mongodb. I'm getting the error "TypeError: Cannot read property 'attributes' of undefined".
This are the models:
UserInterest.js
module.exports = {
attributes: {
id: {
type: 'integer',
primaryKey: true
},
name: { type: 'string' },
profiles: { collection: 'UserProfile', via: 'interests'}
}
};
UserProfile.js
module.exports = {
attributes : {
id : {
type : 'string',
autoIncrement : true,
primaryKey : true
},
createdAt : {
type : 'datetime'
},
updatedAt : {
type : 'datetime'
},
user : {
model : 'User'
},
sex : {
type : 'integer'
},
interests : {
collection : "UserInterest",
via : "profiles",
dominant : true
}
//Other attributes here
}
};
I'm trying to get a user profile loaded this way:
UserProfile.findOne({user : id})
.populate("interests")
.exec(function(err, obj) {
if (err) {
cb(err, null);
} else {
cb(err, obj);
}
});
The error happens in populate function. Why this is happening?
After struggling a lot with this problem, I found an answer. The attribute interests was duplicated and sails was having problems with it. I eliminated the duplicate, leaving the collection definition.

With AngularJS, how can I leverage ng-show against an array to find certain values?

I have a model, Program, that looks something like this
var ProgramSchema = new Schema({
permissions: [{
user: {
type: Schema.ObjectId,
ref: 'User'
},
roles: {
type: [{
type: String,
enum: ['reader', 'editor', 'admin', 'requested']
}]
}
}],
type: {
type: String,
enum: ['public', 'private'],
default: 'public',
required: 'Type cannot be blank'
}
});
As display a list of programs on a page, I want to show an icon if the currently authenticated user $scope.authentication.user is in the program.permissions.user and with a role of reader, editor, or admin.
I was thinking of an ng-show but since programs.permissions is an array, I couldn't make it work.
Any help would be great! thanks!
sample program data
{
"_id" : ObjectId("55ab4acd24640cd55097c356"),
"permissions" : [
{
"user" : ObjectId("55a897dfad783baa677e1326"),
"roles" : [
"reader"
]
},
{
"user" : ObjectId("5563f65a84426d913ae8334e"),
"roles" : [
"editor"
]
}
]
}
Thru some help, here is what I ended up doing
I called a function in my ng-show
ng-show="userFollowedProgram(program)"
In my controller
$scope.userFollowedProgram = function(program) {
//loop thru permissions and see if user is in there.
for (var i = 0; i < program.permissions.length; i++) {
if (program.permissions[i].user === $scope.authentication.user._id) {
//loop thru roles and see if user is following.
if (program.permissions[i].roles.indexOf('admin') > -1 ||
program.permissions[i].roles.indexOf('editor') > -1 ||
program.permissions[i].roles.indexOf('reader') > -1) {
return true
}
}
}
return false;
};
While not be the prettiest, you can explicitly check for your values with the || logical operator on your ng-show using indexOf(). I've mocked a simple example, using a <span> as an "icon" - but you can certainly work the idea into your working copy from here. Observe the following...
<li ng-repeat="user in users">
<span>{{ user.name }}</span>
<span class="ico"
ng-show="user.roles.indexOf('reader') > -1 || user.roles.indexOf('editor') > -1 || user.roles.indexOf('admin') >-1">icon
</span>
</li>
$scope.users = [{
id: 1, name: 'bob', roles: ['reader', 'editor', 'admin']
},{
id: 2, name: 'jane', roles: ['reader']
},{
id: 3, name: 'chris', roles: ['editor']
},{
id: 4, name: 'susy', roles: ['requested'] // sorry susy
}];
JSFiddle Link - simple demo
Another interesting way you can accomplish could include testing for their role via regex and return a truthy value. Could be more overhead than it's worth, but check it out if you wish...
<span class="ico" ng-show="isInRole(user)" >icon</span>
$scope.isInRole = function(user) {
return /(reader|editor|admin)/.test(user.roles.join('|'));
}
JSFiddle Link - regex demo
I was able to figure it out.
I called a function in my ng-show
ng-show="userFollowedProgram(program)"
In my controller
$scope.userFollowedProgram = function(program) {
for (var i = 0; i < program.permissions.length; i++) {
if (program.permissions[i].user === $scope.authentication.user._id) {
return true;
}
}
return false;
};

How to sort a store according to another store in sencha touch

I have two store and i want to sort one store according other store.
Store1 store a model:
fields: [
{
name: 'id',
type: 'string'
},
{
name: 'name',
type: 'string'
}
]
Store2 store model:
fields: [
{
name: 'id',
type: 'string'
},
{
name: 'subject',
type: 'string'
}
]
I sort the store2 by subject and i want to sort store1 according store2's id.
How can i do that?
You need to listen sort event on the first store and accordingly sort the second store. Please take a look at the following fiddle:
https://fiddle.sencha.com/#fiddle/np5

Categories

Resources