add data into combobox using extjs - rest

Hi I have a combo box which have a store that is based on rest call and populate data from data base, its working fine but i need a value 'All' with all the values coming from data base so how can i do this please suggest
var wardName = Ext.create('Ext.data.Store', {
id : 'visittype',
fields : [
{
name : 'id',
type : 'integer',
},
{
name : 'wardName',
}
],
remoteGroup : true,
remoteSort : true,
proxy : {
type : 'rest',
url : 'pmsRest/wards',
reader : {
root : "wardMaster",
idProperty : 'id'
}
},
autoLoad : true,
});
{
xtype : 'combo',
name : 'wardName',
labelStyle : 'color:black;font-weight:bold;width:250px;padding:10;',
labelSeparator : "",
id : 'wardName',
width:'33%',
fieldLabel : 'Ward',
triggerAction : 'all',
store : wardName,
displayField : 'wardName',
valueField : 'id',
multiSelect : false,
typeAhead : true,
//disabled: true,
listeners : {
change : function(combo) {
Ext.getCmp('bedName').bindStore(loadBedName());
}
},
allowBlank : false,
//enableKeyEvents : true,
},

Once the data from the proxy is loaded successfully you can use the insert method of store to add the static data in store.
insert method inserts Model instances into the Store at the given index.
The syntax is store.insert( index, records ) where index is the position at which you want to insert the instance, in this case it would be 0.
hope it helps you. :-)

Add a listener to the store and a load event. In that event you can add a new item to the store when the store is loaded.
listeners: {
load: function(){
this.add({
id: 0,
name: 'All'
});
}
}

Related

Mongoose object schema: How do I declare schema in mongoosejs to be able to iterate an options field?

Currently the data in mogo via mongoose is as below. I want to be able to iterate options once I pull it out of mongo.
currently if I save this record to a variable “record". I would need to do record.options[0].option1.check, I want to avoid [0].
I want to be able to just iterate over an object and be able to do the following.
record.option1.choice1
record.option2.choice2
my schema is
var questionSchema = new mongoose.Schema({
question: String,
question_slug: String,
options: []
});
{
"_id" : ObjectId("5832609483bf491db3d8faa1"),
"question" : “What is the capital of Illinois?",
"question_slug" : “what-is-the-capital-of-illinois",
"options" : [
{
"option1" : {
"check" : "on",
"choice" : “Springfield"
},
"option2" : {
"check" : "",
"choice" : “Chicago"
}
}
],
"__v" : 0
}
You can't define the schema for record.option1.choice1 and record.option2.choice2 since option1 and option2 seems to be dynamic.
But you can avoid [0] for record.options[0].option1.check by defining options as Object in the schema instead of array as below.
Hence, you can iterate over the object record.options with record.options.option1 and record.options.option2 etc.
var questionSchema = new mongoose.Schema({
question: String,
question_slug: String,
options: {
type: Object
}
});
{
"_id" : ObjectId("5832609483bf491db3d8faa1"),
"question" : “What is the capital of Illinois?",
"question_slug" : “what-is-the-capital-of-illinois",
"options" : {
"option1" : {
"check" : "on",
"choice" : “Springfield"
},
"option2" : {
"check" : "",
"choice" : “Chicago"
}
},
"__v" : 0
}

How To Structure Dynamic MongoDb Query

Please i am trying to wrap my head over how to query a collection dynamically. I have a collection that has the below schema.
Tripart_Property_Schema = new SimpleSchema({
"type" : {
type : String,
label : 'Property type',
},
"sale_lease" : {
type : String,
label : '',
},
"location" : {
type : Object,
label : '',
optional : true
},
"location.state" : {
type : String,
label : 'state',
},
"location.lga" : {
type : String,
label : 'lga',
optional : true
},
"location.address" : {
type : String,
label : 'address',
optional : true
},
"amount" : {
type : Object,
label : '',
optional : true
},
"amount.amount" : {
type : Number,
label : '',
optional : true
},
"amount.discount" : {
type : Number,
label : '',
optional : true
},
"active" : {
type : Boolean,
label : '',
optional : true
},
"views" : {
type : Number,
label : '',
optional : true
},
"date_added" : {
type : Date ,
label : '',
optional : true
},
"general_features" : {
type : [String],
label : '',
optional : true
},
"outdoor_features" : {
type : [String],
label : '',
optional : true
},
"indoor_features" : {
type : [String],
label : '',
optional : true
},
"other_facilities" : {
type : Object,
label : '',
optional : true
},
"other_facilities.bedroom" : {
type : Number,
label : '',
optional : true
},
"other_facilities.bathroom" : {
type : Number,
label : ' ',
optional : true
},
"other_facilities.garage" : {
type : Number,
label : '',
optional : true
},
"other_facilities.staffQuaters" : {
type : Number,
label : '',
optional : true
}
});
I have created a user interface where a user can query the data using any combination of the available fields. A user can make a query searching for a property by using the sale_lease field, amount.amount field and also query the general_features field which is an array. I don't know how to generate this query based on the user selected preference. I know how to perform the query knowing the fields queried before hand, but making it dynamic is where the problem lies.
I am trying to make use of multiple if statements to possibly make a query for all possible combination of fields, but i kind of realize that this is not the way to achieve this. I will be very grateful if i can be pointed to the right direction.
Let's take the simple case where your collection has several keys, the user can query on any combination of them, and you want to AND the results. A typical pattern would be:
let query = {}'
if ( $("#type").length ) query.type = $("#type").val();
if ( $("#sale_lease").length ) query.sale_lease = $("#sale_lease").val();
if ( $("#state").length ) query.location = { state: $("#state").val() };
return Tripart_Property.find(query);
For simplicity, the above assumes that you've given each search field an ID equal to the corresponding schema field. Your naming convention may vary.
As you can see, you start with a blank query object then look at each search field, if it is non-blank then you add a key to the query corresponding to the schema key you want to search on.
With good naming conventions and a simple field structure you can even do this in a js loop so you don't need one if statement per key.
Without knowing the specifics of how users are able to make a selection for the query, the name of the collection, etc, it's difficult to provide a detailed example; however, you should be able to take the user's selection and turn it into a query parameter object. For example, assuming the collection name is Tripart_Property, if the user makes a selection for the sale_lease field, you could do the following:
var saleLeaseSelection = argumentContainingSelectionFromUser;
var customQuery = { sale_lease: saleLeaseSelection };
Tripart_Property.find(customQuery);
This is a very basic example. argumentContainingSelectionFromUser is the data from the user selection. You would probably want to build a method on the server that you could call from the client that would return the results from the query so that you could display them to the user, but this should at least get you pointed in the right direction.

Post object from backbone to REST

I am trying to post data from front-end to RESTful. I'm a backbone beginner, so my problems are probably easy to solve. But have struggled with this all day - so now I'm asking for guidance.
I have a button that use to add data to REST. So in my view I use object.save(); to save an object to model.
Here is my model:
define(["underscore" , "backbone"],function(_ , Backbone){
var Test = Backbone.Model.extend({
url:'http://mysite.com/Test/Test.svc/AddTest',
idAttribute: 'ID'
});
return Test;
});
View :
define(["jquery" ,
"underscore" ,
"backbone" ,
'models/Test',
'views/Test',
],function($ , _ , Backbone , Test, TestView){
var HomeView = Backbone.View.extend({
initialize: function() {
//....
},
events : {
"click #byn" : function(){
//....
},
'click #test' : 'addTest'
},
addTest : function(){
var object = new Test();
object.set({
"ID" : 0,
"Name" : "",
"CustomerID" : 106,
"Type" : 0,
"LastUpdated" : "\/Date(1383152400000+0700)\/",
"Detail" : [
{
"ID" : 0,
"TID" : 0,
"ItemID" : 22776,
"Quantity" : 2,
"LastUpdated" : "\/Date(1383152400000+0700)\/"
}
]
});
object.save();
var _wlView = new TestView({model:object});
},
render : function(){
//....
}
});
return HomeView;
});
To be truth, I really don't know how could I post the object that I have saved in my view object.save(); to the restful throw the rest url like http://mysite.com/Test/Test.svc/AddTest.
Url of model should be declared as urlRoot value and it is better to have it relative (e.g. "/AddTest").
Then the backend route for save action will be "actual url" + "urlRoot".

'this.el is null' while creating drag-drop feature in treepanel of ExtJS 4

I am trying to implement drag-n-drop feature in treepanel of ExtJS 4. Basically I want to drag some nodes from treepanel into an text box. I am pretty confused with the way drag-n-drop is implemented in ExtJS 4 but then also I have tried to write some code. I am not sure whether its correct or not.
My code is as follows :
CustomChart.js file contents
Ext.define('dd.view.CustomChart', {
extend : 'Ext.panel.Panel',
alias : 'widget.customChart',
layout : {
type : 'vbox',
align : 'stretch'
},
initComponent : function() {
this.items = [
{
xtype : 'textfield',
name : 'values',
fieldLabel : 'Drop values here'
}
];
this.callParent(arguments);
}
});
I am using this CustomChart panel inside AttritionViewer file as follows :
Ext.define('dd.view.AttritionViewer', {
extend : 'Ext.panel.Panel',
alias : 'widget.attritionViewer',
title : 'View attrition by dimensions',
layout : 'border',
initComponent : function() {
this.items = [
{
xtype : 'treepanel',
title : 'Select dimensions',
store : 'Dimensions',
rootVisible : false,
region : 'west',
height : '100%',
width : '20%',
viewConfig : {
plugins : {
ptype : 'treeviewdragdrop',
ddGroup: 'GridExample'
}
}
},
{
xtype : 'panel',
region : 'center',
layout : {
type : 'vbox',
align : 'stretch'
},
items : [
{
xtype : 'customChart',
flex : 1
}
]
}
];
this.callParent(arguments);
}
});
As you can see in code above, I have set ViewConfig and ddGroup for treepanel. Now I am not sure where to put following code so I have put it in init() method of controller. init() method of my controller looks as follows :
var customChartEl = Ext.widget('customChart');
var formPanelDropTarget = Ext.create('Ext.dd.DropTarget', customChartEl, {
ddGroup: 'GridExample',
notifyEnter: function(ddSource, e, data) {
console.log('inside notifyEnter() method');
//Add some flare to invite drop.
/* formPanel.body.stopAnimation();
formPanel.body.highlight(); */
},
notifyDrop : function(ddSource, e, data){
console.log('inside notifyDrop() method');
return true;
}
});
After this code, I am getting this.el is null error at ext-debug.js (line 7859). I dont know what to do next.
Please guide me on how to drag an node from treepanel inside text field.
Thanks in advance !!!
check this link,
http://examples.extjs.eu/?ex=tree2divdrag
i am also trying one similar task. i can help you if i get some output.
if you solve your problem, just make a note here, that will help me too.
check the source of this sample too. http://docs.sencha.com/ext-js/4-0/#!/example/dd/dragdropzones.html.
Best wishes.

Mongoose update / schema design

I am trying to update a mongoose model. Having some difficulites. This is how my schema looks like right now.
I want a user to have a library which consists of many songs. Those songs can be in multiple users libraries. And can also be in multiple playlists of which a user can have several.
var LibrarySchema = new Schema({
user: Schema.ObjectId
});
var PlaylistSchema = new Schema({
title: String,
description: String,
//songs: [SongsSchema],
user: Schema.ObjectId
});
var SongsSchema = new Schema({
name: String,
artist: String,
album: String,
time: Number,
url: String,
gid: String,
location: String,
playlist: [{playlist_id:Schema.ObjectId, position: Number}],
library: [Schema.ObjectId]
});
Now I want to do something like check if the url already exists in the song schema (url is an index). If that song does not exist I want to add it with all the values. However, if the song exists I want to append the playlist and position its in and the library.
For example here is the query I am trying now:
Song.update({url: s.url, "playlist.playlist_id": playlistId}, {$set: {name: s.name, artist: s.artist, album: s.album, time: s.time, url: s.url}, $addToSet: {playlist: {playlist_id: playlistId, position: s.position}, library: libId}},{upsert: true}, function(err, data) {});
And this is a sample of the database stored:
{ "name" : "Kanye West - All Of The Lights ft. Rihanna, Kid Cudi", "artist" : "unknown", "album" : "unknown", "time" : 328, "url" : "HAfFfqiYLp0", "_id" : ObjectId("4f127923ce0de70000000009"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
"playlist_id" : ObjectId("4f127923ce0de70000000007"),
"position" : "1"
}
] }
{ "name" : "Kanye West - Heartless", "artist" : "unknown", "album" : "unknown", "time" : 221, "url" : "Co0tTeuUVhU", "_id" : ObjectId("4f127923ce0de7000000000a"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
"playlist_id" : ObjectId("4f127923ce0de70000000007"),
"position" : "2"
}
] }
{ "name" : "Kanye West - Stronger", "artist" : "unknown", "album" : "unknown", "time" : 267, "url" : "PsO6ZnUZI0g", "_id" : ObjectId("4f127923ce0de7000000000b"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
"playlist_id" : ObjectId("4f127923ce0de70000000007"),
"position" : "3"
}
] }
Now what I am trying to do is if the url for the song is not in the database it should add it with all the information. If it is not then it will add to the library (which each user has one of) the library id UNLESS it already contains that value.
That works pretty good. The problem I am having right now is when it finds the url I want it to add to playlist the playlistId of the current playlist UNLESS it is already in the array and if it is and was passed a new value for position (user can change the order of a playlist) it should change the value of the position.
Perhaps I am not modelling it in a proper way. Any tips would be awesome!
MongooseJS contains something called DBRef. I think you want to use that, instead of handling all the references yourself.
For inserting if something is not there, either do a lookup first and insert if needed, or use an upsert. Not sure if Mongoose supports Upserts though...