Ember.Select default option - select

How do I set the default option for a select box when the page loads? I've tried setting the default value for the selectedPersonController in the example below but to no avail. Am I doing something wrong?
var App = Ember.Application.create();
App.Person = Ember.Object.extend({
id: null,
firstName: null,
lastName: null,
fullName: function() {
return this.get('firstName') + " " + this.get('lastName');
}.property('firstName', 'lastName').cacheable()
});
App.selectedPersonController = Ember.Object.create({
person: null
});
App.peopleController = Ember.ArrayController.create({
content: [
App.Person.create({id: 1, firstName: 'Yehuda', lastName: 'Katz'}),
App.Person.create({id: 2, firstName: 'Tom', lastName: 'Dale'}),
App.Person.create({id: 3, firstName: 'Peter', lastName: 'Wagenet'}),
App.Person.create({id: 4, firstName: 'Erik', lastName: 'Bryn'})
]
});
Template:
{{view Ember.Select
contentBinding="App.peopleController"
selectionBinding="App.selectedPersonController.person"
optionLabelPath="content.fullName"
optionValuePath="content.id"}}

You don't set a specific person on your App.selectedPersonController. See a working example here: http://jsfiddle.net/ZpTYV/
// set a default selected person
App.selectedPersonController.set('person', App.peopleController.objectAt(1));
I don't know if this is a typo in your example, but you also have to declare App globally, so it can be accessed in the Handlebars template.

Related

Where to find Mongo Collection in Express?

I'm reviewing some old code I wrote in Express/Mongo/Mongoose (based on an online tutorial) and can't locate the portion of code which dictates which Collection in MongoDB gets written to.
I have a Database, UsersDB and within that database there are several Collections. The Collection that keeps growing every time the code executes user.save is the users Collection. But I can't find any reference in my code base to the users collection.
The only place in my code where I save a User is:
var app = require('../app');
var util = require('util');
var User = require('../models/user'),
Auth = User.Auth,
Name = User.Name,
Email= User.Email,
Phone = User.Phone,
Address = User.Address,
Company = User.Company,
PersonalData = User.PersonalData,
Id = User.Id,
Photo = User.Photo,
Member = User.Member,
CreditCard = User.CreditCard,
UserObj = User.User;
var moment = require('moment');
var async = require('async');
. . .
. . .
exports.user_create_post = [
(req,res, next) => {
console.log("Request: " + util.inspect(req.body));
},
//VALIDATE
body('mainEmail', 'Must be valid email.').isLength({min: 5}).trim(),
//SANITIZE
sanitizeBody('*').escape(),
//POPULATE NEW DOCUMENT
(req,res,next) => {
const errors = validationResult(req);
var auth = new Auth(
dateEffective: {value: moment(Date.now()).format("YYYY-MM-DD hh:mm:ss SSS"), attr: {hidden: true, label: ""}},
username: {"value": req.body.username, "attr": {hidden: false, label: "Username: "}},
password: {"value": req.body.password, "attr": {hidden: false, label: "Password: "}},
mainEmail: {"value": req.body.mainEmail, "attr": {hidden: false, label: "Email: "}}
});
var user = new UserObj(
{authData: [auth]}
);
if (!errors.isEmpty()) {
const errorFormatter = ({ location, msg, param, value, nestedErrors }) => {
// Build your resulting errors however you want! String, object, whatever - it works!
return `${location}[${param}]: ${msg}`;
};
const result = validationResult(req).formatWith(errorFormatter);
if (!result.isEmpty()) {
return res.json({ errors: result.array() });
}
}
else {
user.save(function(err){
if (err) { return next(err);}
});
res.redirect("http://localhost:4200/two-fa/"+user._id);
}
}
I also have a Models module (user.js):
. . .
. . .
. . .
module.exports = {
Auth: mongoose.model('Auth', AuthSchema),
Name: mongoose.model('Name', NameSchema),
Email: mongoose.model('Email', EmailSchema),
Phone: mongoose.model('Phone', PhoneSchema),
Address: mongoose.model('Address', AddressSchema),
Company: mongoose.model('Company', CompanySchema),
PersonalData: mongoose.model('PersonalData', PersonalDataSchema),
Id: mongoose.model('Id', IdSchema),
Photo: mongoose.model('Photo', PhoneSchema),
Member: mongoose.model('Member', MemberSchema),
CreditCard: mongoose.model('CreditCard', CreditCardSchema),
User: mongoose.model('User', UserSchema)
}
I did a search on my entire code, and nowhere is there any mention of users, which is the Collection that's getting written to.
Where should I look to try to trace how the users collection is getting written to?
Thank you!
As per how-to-access-a-preexisting-collection-with-mongoose, if a 3rd argument to mongoose.model is not provided, then Mongo automatically "pluralizes" the model name to give the name of the Collection.

How to disable specific options in a select in Groovy Server Pages (gsp)

I have the below select tag in my gsp which is working fine.
<q:select id="myItem"
name="myItem"
from="${items}"
optionKey="id"
value="${item?.id}"
optionValue="${{it?.name)}}"
noSelection="${['': '']}" />
But I need to disable couple of options within the select based on its status attribute.
items = [{
id: 1,
name: 'test',
status: 'available'
},
{
id: 2,
name: 'john',
status: 'booked'
},
{
id: 3,
name: 'sans',
status: 'available'
}];
I want the option item with status booked to be shown as readonly or disabled. How to achieve this.
You just need to add an optionDisabled attribute like:
optionDisabled="${{it?.status == 'booked'}}
to your <g:select>.
EDIT: for anyone coming to this later, note #HansMaulwurf comment below noting that you'll need to use optionKey as well.

Fill PDF form using perl or node

I need to fill PDF form, Which contains radio buttons too. I tried to fill it with perl, CAM::PDF module and with node, pdf-fill-form module.
In node, the radio's value is always undefined:
{
name: 'Op1',
page: 14,
value: undefined,
id: 983128,
type: 'radio'
}
In perl, the object's value looks like this (if the first option is checked is S, second: XL, third: 2XL etc):
'V'=> bless({
'gennum'=>0,
'value'=>'S',
'type'=>'label',
'objnum'=>988
},
'CAM: : PDF: : Node')
If I change the S to XL, nothing happens in the PDF.
Has somebody any idea, How to fill the radio box?
You can do with node js npm pdf-fill-form module.
let say you are trying to set gender:
your radio button must be formed this way.
{ name: 'gender',
page: 0,
value: false,
id: 65558,
caption: 'male',
type: 'radio' },
{ name: 'gender',
page: 0,
value: false,
id: 65559,
caption: 'female',
type: 'radio' }
Here caption is the important one.
if you trying to set gender is male means just do this
{ gender: 'male'}

Meteor, facebook login, different values returned : what about the profile picture?

On a meteor app, when a log in with facebook on localhost, I obtain those fields for theuser.services.facebook:
{
accessToken: 'long_acces_tkoen_string',
expiresAt: --,
id: '--',
email: '--',
name: '--',
first_name: '--',
last_name: '--',
link: 'https://www.facebook.com/--',
username: '--',
gender: '--',
locale: '--'
}
When I deploy my app on https://modulus.io/, these fields are returned :
{
accessToken: '--',
expiresAt: --,
id: '--',
email: '--',
name: '--',
first_name: '--',
last_name: '--',
link: 'https://www.facebook.com/app_scoped_user_id/--/',
gender: '--',
locale: '--'
}
No userneame, so, no profile picture that I used to build like that : 'https://graph.facebook.com/' + user.services.facebook.username + '/picture?width=100&height=100'
How do I rebuild that ?
I understand that this is probably caused by this : https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api
But I do not understand why (and why does it work on localhost and not on modulus)
Well, actually, you can simply use the id field. I never noticed that.
But users have a new id. For instance, for me, these thre urls are working :
// old id field :
https://graph.facebook.com/558820281/picture?width=200&height=200
// old username field :
https://graph.facebook.com/huet.fabien/picture?width=200&height=200
// new id field
https://graph.facebook.com/10154176840535282/picture?width=200&height=200

Map Reduce Mongodb Node JS native driver

I'm working with the Mongodb native driver using a Map reduce function. Basically I have a mediaId as a key and want to count how many medias loaded and started per mediaId.
So what I've done was:
var map = function(){
emit(this.media.id, {
count: 1,
played: 0,
ph: this.project.id,
title: this.media.title,
media: this.media.id,
origin: this.origin,
thumbnail: this.media.thumbnail,
mediaDuration: this.media.mediaDuration,
state: this.state
});
};
var reduce = function(k, vals) {
result = {
count: 0,
played: 0,
ph: '',
title: '',
media: '',
origin: '',
thumbnail: '',
mediaDuration: 0,
state: ''
};
vals.forEach(function(doc){
result.count += doc.count;
result.ph = doc.ph;
result.title = doc.title;
result.media = doc.media;
result.thumbnail = doc.thumbnail;
result.mediaDuration = doc.mediaDuration;
result.state = doc.state;
result.origin = doc.origin;
if(doc.state === "started") {
result.played += 1;
}
});
return result;
};
In my test collection I have 2 different mediaIds. One with 553 objects and another one with just 1 object. I've putted all in the "started" state to test this so basically the number of count should be equal to the number of played.
When I run the Map/Reduce function it returns to me ( I used the "toArray" function of the mongodb native driver):
[ { _id: '12398asdsa9802193810asd120',
value:
{ count: 1,
played: 0,
ph: '123213ased12231',
title: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
media: '1xxxxxxxxxxxxxxxxxxxxxxxxxxx1',
origin: 'http://www.google.com',
thumbnail: 'http://cache.ohinternet.com/images/0/0e/Forever_Alone.png',
mediaDuration: 12321321,
state: 'started' } },
{ _id: '2c9f94b42f5b5114012f5b92ea430066',
value:
{ count: 553,
played: 155,
ph: '316',
title: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
media: '2xxxxxxxxxxxxxxxxxxxxxxxxxxx2',
origin: 'http://localhost:9000/views/index.html',
thumbnail: null,
mediaDuration: null,
state: 'started' } } ]
It seems that one I have just one object the reduce function isn't called ( I did some tests with another collection with more than 100 mediaIds and the behavior was identical. Does anyone have an idea of what is wrong with that?
Thanks A LOT for your time,
Cheers.
I sort of solved the "issue".
I did the filter on the Map Function and not on the Reduce function. Something like this:
var map = function(){
if(this.media.state==="started") {
var played = 1;
}else{var played = 0;}
emit(this.media.id, {
count: 1,
played: played,
ph: this.project.id,
title: this.media.title,
media: this.media.id,
origin: this.origin,
thumbnail: this.media.thumbnail,
mediaDuration: this.media.mediaDuration,
state: this.state
});
};
Hope it helps anyone that is having the same "problem"