creating extjs store data record is failing - javascript

I have the error Uncaught TypeError: Cannot read property 'items' of undefined
The following line where I create the NewEditInfo type.
The alert shows there are values in the arrays so I have no clue..
getNewEditInfo : function () {
var values = {};
var form = this.getForm();
var formValues = form.getValues();
for (var fieldName in formValues)
values[fieldName] = (formValues[fieldName] != "") ? formValues[fieldName] : null;
alert(JSON.stringify(values));
alert(JSON.stringify(formValues));
NewEditInfo = Ext.data.Record.create([{
name : "id",
type : "string"
}, {
name : "hardwareId",
type : "string"
}, {
name : "location",
type : "string"
}, {
name : "active",
type : "string"
}
]);
var newEditInfoInstance = new NewEditInfo({
id : values['idField'],
hardwareId : values['hardwareIdField'],
location : values['location '],
active : values['active']
});
return newEditInfoInstance;
}
Can anyone help please?

Ext.data.Record.create() does not return model class but new model instance.
Firstly you should define your own model:
Ext.define('editInfoModel', {
extend: 'Ext.data.Model',
fields: [
{
name : "id",
type : "string"
}, {
name : "hardwareId",
type : "string"
}, {
name : "location",
type : "string"
}, {
name : "active",
type : "string"
}
]
});
Then in your getNewEditInfo method you can create new instance of editInfoModel and set record data by Ext.create() method:
var newEditInfoInstance = Ext.create('editInfoModel', {
id : values['idField'],
hardwareId : values['hardwareIdField'],
location : values['location '],
active : values['active']
});

Related

Mongoose - go through object

Using mongoose on node.js I'm trying to find all games where player game.players.id equals the id I passed.
Schema:
var Game = mongoose.Schema({
id: String,
date: { type: Date, default: Date.now },
game: Object,
isOnline: Boolean
});
I'm not sure what is wrong in this function but it returns empty array:
var specificGameStatistics = function (user, game) {
var deferred = q.defer()
Game.find({ "game.players.id" : user, "game.rules.gameType": game.gameType, "game.rules.quatro": game.quatro}, function(err, data) {
deferred.resolve(data);
});
return deferred.promise;
}
////////////////////USAGE///////////////
var testGame = {rules: {gameType : 1, quatro : null}}
UsersCtrl.specificGameStatistics(data.id, testGame).then(function(userData) {
console.log(userData);
});
And here is the example of the game already saved in database:
{
"isOnline" : true,
"game" : {
"numberOfPlayers" : NumberInt("1"),
"players" : [
{
"id" : "58a2c0ecd8ba9f8602836870",
"name" : "PlayerName",
"type" : NumberInt("1"),
"avgStatistic" : "30.00",
"numbersHit" : NumberInt("1"),
"totalScore" : NumberInt("60"),
..............................
}
], //there is more players here
"rules" : {
"gameType" : NumberInt("1"),
"quatro" : null,
"rounds" : NumberInt("1"),
} // there is more in JSON object
...............................
"_id" : ObjectId("58aed4aeea20ecdf0c426838"),
"date" : ISODate("2017-02-23T13:25:18.284+01:00"),
"__v" : NumberInt("0")
}
I have tested the player ID to be equal and it is but still it returns empty array. Test code:
///////////TEST//////////////
console.log(data.id, "58a2c0ecd8ba9f8602836870");
if (data.id === "58a2c0ecd8ba9f8602836870") {console.log("this is true");}
var testGame = {rules: {gameType : 1, quatro : null}}
UsersCtrl.specificGameStatistics(data.id, testGame).then(function(userData) {
console.log(userData);
});
//////////TEST///////////////
and it returns:
58a2c0ecd8ba9f8602836870 58a2c0ecd8ba9f8602836870
this is true
[]
--------------------------------------------------------------------------------------------------------
Answer: With help of Deividas Karžinauskas the solution is:
Game.where('game.players.id', user).where('game.rules.gameType', game.rules.gameType).find({}, function(err, data) { //, "game.rules.quatro": game.quatro
deferred.resolve(data);
});
This is because of the additional rules that you specify ({gameType : 1, quatro : null}), which do not exist in the player object (
{
"id" : "58a2c0ecd8ba9f8602836870",
"name" : "PlayerName",
"type" : NumberInt("1"),
"avgStatistic" : "30.00",
"numbersHit" : NumberInt("1"),
"totalScore" : NumberInt("60"),
..............................
}
). You can confirm this by simply looking for a game by id.
If you want to add these rules then you should find all games which match these rules and then look for the games of a specific player.

Accessing single field from object in array

I'm using Meteor with MongoDB and can't seem to figure out how to access a single field from the objects in an object array.
My documents:
{
"_id" : "p6c4cSTb3cHWaJqpG",
"createdAt" : ISODate("2016-05-11T11:30:11.820Z"),
"username" : "admin",
"contacts" : [
{
"when" : ISODate("2016-05-11T11:30:32.350Z"),
"who" : "4YBufbE9PByJBkasy"
},
{
"when" : ISODate("2016-05-25T11:52:49.745Z"),
"who" : "z792kEEYbxyzyEAKp"
},
{
"when" : ISODate("2016-05-26T13:47:43.439Z"),
"who" : "4YBufbE9PByJBkasy"
},
{
"when" : ISODate("2016-05-26T13:48:22.828Z"),
"who" : "4YBufbE9PByJBkasy"
}
]
}
I want to check if a userId is in any of the objects, specifically in the who fields.
My Server-side code:
var me = Meteor.userId();
var findMe = Meteor.users.findOne(me);
if (_.include(findMe.contacts, {who: 4YBufbE9PByJBkasy})){
console.log("found in array");
}else{
console.log("Not found in array");
}
}
I have tried this several different ways, but came up with nothing.
When I console.log(findMe.contacts);, it returns the whole array like it should. but when I try to console.log(findMe.contacts.who);, it returns undefined.
Just need some direction on how to access the field of the object array. Thanks!
Looping through an array to see whether it contains a value is easily
done with Array.prototype.some:
var data = {
"_id" : "p6c4cSTb3cHWaJqpG",
"createdAt" : "2016-05-11T11:30:11.820Z",
"username" : "admin",
"contacts" : [
{
"when" : "2016-05-11T11:30:32.350Z",
"who" : "4YBufbE9PByJBkasy"
},
{
"when" : "2016-05-25T11:52:49.745Z",
"who" : "z792kEEYbxyzyEAKp"
},
{
"when" : "2016-05-26T13:47:43.439Z",
"who" : "4YBufbE9PByJBkasy"
},
{
"when" : "2016-05-26T13:48:22.828Z",
"who" : "4YBufbE9PByJBkasy"
}
]
};
var hascontact = function(contacts, id){
return contacts.some(function(contact){
return contact.who === id;
});
};
console.log(hascontact(data.contacts,'4YBufbE9PByJBkasy'));
console.log(hascontact(data.contacts,'z792kEEYbxyzyEAKp'));
console.log(hascontact(data.contacts,'asdfasdfasdfasdfa'));

How to remove selected existing data and add new row without affecting other rows in Jquery data table

I have 100 rows of data, i want to remove selected data based on ID and add new row for that ID. I want to edit the rows based on ID.
Here is my code:
oCustomization : {
sExportFunctionCall : oMapUrls.exportLiveFleetReport,
bAdvanceExport : true,
bShowDefaultAll : !bLivePaginate
},
pageLength : !bLivePaginate ? -1 : Global.rowLength,
scrollCollapse : false,
scrollY : iDataTableHeight,
serverSide : bLivePaginate,
order : [ [ 3, "desc" ] ],
columns : [
{
"data" : "trackeeName",
"width" : aColumnWidths[0],
"class" : "no-word-break",
"settings" : {
source : function(request, oCallback) {
oCallback($.ui.autocomplete.filter(Global.aJSTreeVehicleItems || [], request.term));
}
},
"render" : function(value, type, rowData) {
//Some code here
}
},
{
"data" : "firstName",
"width" : aColumnWidths[1],
"class" : "no-word-break",
settings : {
source : Global.getDriverSuggestion
},
"title" : jQuery.i18n.prop("driver.title.txtInfo"),
"visible" : Global['show.driver.in.reports'] == 1,
"render" : function(value, type, rowData) {
return getUserName(rowData.firstName, rowData.lastName);
}
},
{
"data" : "groupName",
"width" : aColumnWidths[2],
"class" : "no-word-break",
"settings" : {
source : function(request, oCallback) {
oCallback($.ui.autocomplete.filter(Global.aJSTreeGroupItems || [], request.term));
}
},
"title" : jQuery.i18n.prop("vehicle.col2label")
},
{
"data" : "dateAndTime",
"width" : aColumnWidths[3],
"searchable" : !bLivePaginate,
"class" : "wordBreak",
"title" : jQuery.i18n.prop("report.columnTitle.date"),
"render" : function(value, type, rowData) {
if (type == "display") {
return rowData.formattedDate;
}
return value;
}
}
I get data in JSON format. I want to remove selected rows , it should not affect other rows data.
Simply use WHERE
DELETE FROM DBNAME WHERE ID=XXX
then Insert into the table with that ID
however, if you want to delete and insert together always, an UPDATE query will work better

Data not shown in slickgrid

I'm working with slickgrid in my webpage, the problem I'm getting while using it that I'm unable to see the data in the grid. While when I try to get the data through grid object (i.e. grid.getData();) , I can get the whole data which I have previously set in the grid but don't know why its not showing up. May be I'm missing some thing.
following is my code for drawing the grid.
HTML code:
<div id="myGrid" width="100%"; height="500px"></div>
JS code:
drawBlotterForOrders : function(data){
try{
var columns = this.getBlotterColumns();
var options = {
enableCellNavigation: true,
enableColumnReorder: false
};
var dataArray = [];
dataArray = self.setDataInGrid(data);
gridObj = new Slick.Grid("#myGrid" , dataArray , columns , options);
console.log(gridObj.getData());//here I'm able to get the data
}catch(exp){
}
},
setDataInGrid : function(data){
try{
var dataArray = [];
i++;
dataArray[0] = {
id : i,
clOrdId : data.clOrdId,
cumQty : data.cumQty,
execId : data.execId,
execType : data.execType,
leavesQty : data.leavesQty,
ordStatus : data.ordStatus,
orderId : data.orderId,
orderQty : data.orderQty,
side : data.side,
symbol : data.symbol
};
return dataArray;
}catch(exp){
}
},
getBlotterColumns : function(){
var col = [
{ id: 'id',
name : 'id',
field : 'id'
},{
id: 'clOrdId',
name : 'clOrdId',
field : 'clOrdId'
},{
id: 'cumQty',
name : 'cumQty',
field : 'cumQty'
},{
id: 'execId',
name : 'execId',
field : 'execId'
},{
id: 'execType',
name : 'execType',
field : 'execType'
},{
id: 'leavesQty',
name : 'leavesQty',
field : 'leavesQty'
},{
id: 'ordStatus',
name : 'ordStatus',
field : 'ordStatus'
},{
id: 'orderId',
name : 'orderId',
field : 'orderId'
},{
id: 'orderQty',
name : 'orderQty',
field : 'orderQty'
},{
id: 'side',
name : 'side' ,
field : 'side'
},{
id: 'symbol',
name : 'symbol',
field : 'symbol'
}
];
return col;
}
I'm unable to detect the mistake I'm making. I'll be really thankful for any help.
You forgot to render the grid I think, did you put a grid.render() ?

How can I return only filtered model in Emberjs?

I have a Ember.Select that filters the model based on certain roles. At the moment, the when the app initiates the full contents of the model are shown but I want to show the model only when the filters are applied. I am clueless on how I am gonna do that.
Here's my controller in the question,
App.TwodController = Ember.ArrayController.extend({
//filteredContent : null,
sortProperties: ['firstname'],
sortAscending: true,
selectedExperience : null,
experience : [{
exp : "1"
}, {
exp : "2"
}, {
exp : "3"
}, {
exp : "4"
}, {
exp : "5"
}],
selectedDesignation : null,
filterDesignation : function() {
var designation = this.get('selectedDesignation.designation');
var filtered = this.get('content').filterProperty('designation', designation);
this.set("filteredContent", filtered);
}.observes('selectedDesignation'),
designations : [{
designation : "Design",
id : 1
}, {
designation : "Writer",
id : 2
}, {
designation : "Script",
id : 3
}, {
designation : "Storyboard",
id : 4
}, {
designation : "Workbook",
id : 5
}],
actions : {
filterExperience : function() {
var experience = this.get('selectedExperience.exp');
var filtered = this.get('content').filterProperty('experience', experience);
this.set("filteredContent", filtered);
},
refresh : function() {
var refresh = this.get('content');
this.set("filteredContent", refresh);
}
},
filteredContent : function() {
var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');
return this.get('model').filter(function(item) {
var fullname = item.firstname + item.lastname;
return regex.test(fullname);
});
}.property('searchText', 'model')
});
Moreover, one more issue I am having is I am not able to sort by ascending order. What changes I need to do in my code to achieve the desired result?
Here's the full JSBin if anyone's interested.
In your App.TwodController include:
init: function() {
this.set('filteredContent', []);
},
And in filterDesignation change this.get('content') to this.get('arrangedContent') as per the Ember.SortableMixin documentation.
filterDesignation : function() {
var designation = this.get('selectedDesignation.designation');
var filtered = this.get('arrangedContent').filterProperty('designation', designation);
this.set("filteredContent", filtered);
}.observes('selectedDesignation'),

Categories