Nesting HTML in a string with mithril - html

I would like to generate the following HTML with Mithril's m:
<p>I am a <code>person</code></p>.
I'm currently using m.trust for this:
m.trust("<p>I am a <code>person</code></p>").
But I don't like the HTML string. Is there a better way to do this?

Yes, use m function to do this:
m('p', [
'I am a ',
m('code', 'person')
])
See a whole component here: https://jsfiddle.net/rtkhanas/02adbhkt/

Where the html string come from?
If you are writing your view, then use m('p', ...) instead of m.trust
For example, if only the "person" value is dynamic, you should have something like:
window.WhoAmI = {}
WhoAmI.controller = function(attr) {
var ctrl = this
ctrl.gender = m.prop(attr.gender)
}
WhoAmI.view = function(ctrl) {
return m('p', [
'I am a ',
m('code',
ctrl.gender()
)
])
}
If you get the whole html string from a request, then it's probably a bad thing, and you should try to rewrite your API (if possible) to send only the dynamic value to the client.

You should have something like this:
m.module(document.body, {
view: function() {
return m('p', [
'I am a ',
m('code', 'person')
]);
}
})

Related

Access query fields in mongodb inside forEach

In mongodb, we can use forEach() on find() method for iterating over documents inside the collection . Now the question is can we access the query field inside the forEach . For eg:-
db.mycollection.find({name : 'Mike'}).forEach(function(document) {
// var firstName = name;
})
Can we do something like this or what alternative we can use ? Please help.
You are almost there, try the below code:
db.mycollection.find({name : 'Mike'}).forEach(function(document) {
var firstName = document.name; // Just use the . operator
print(firsName)
// Rest of your operations.
})

HTML shows special character in the wrong order

I have a string that I want to display in the HTML.
But if the first character is a special character the HTML displays it in the wrong order.
for example if the string is :
#test : Twitter
I see it in the browser as:
I tried to change the direction using SCSS and adding space but still looks the same.
how do I display it correctly?
Thanks for any help :)
edit:
I'm working with Angular 8 and redux.
I'm creating the string in this selector:
const getMediaOutlet = createSelector<InquiryDetailsPartialState, InquiryDetailsState, BasicInfoDictionaryModel>(
getInquiryDetailsState, (state: InquiryDetailsState) => {
const mediaOutlets: MediaOutletsModel[] = JSON.parse(state.data.mediaOutlets);
return mediaOutlets && mediaOutlets.length ? {
titleTranslation: 'INQUIRIES.DETAILS.BASIC_INFO.MEDIA_OUTLETS',
value: mediaOutlets.map<DictionaryValueModel>(item => {
return { data:" "+item.text +" : "+ item.typeName } //<- here i build the string ;
}).filter(source => {
return source.data && source.data.length;
})
} : { value: [], titleTranslation: '' };
});
i used :
<bdi> </bdi>
and it maked at the right order.
you can do it this way using simple HTML. You don't have to mess up with any CSS as you can do with the only HTML
Example:-
<p>Start "&commat;" End.</p>

ES6 curly braces usage when extracting from an object and assigning to another object

I would like to use curly braces to replace this code:
let rUsers = children.map((user) => {
let rUser= {};
rUser.firstName = user.firstName;
rUser.lastName = user.lastName;
rUser.relation = user.patron.relation;
return rUser;
});
This is what I have:
let rUsers = children.map((user) => {
let rUser = {};
({
firstName: rUser.firstName,
lastName: rUser.lastName,
patron.relation: rUser.relation, // I get an error here
} = user);
return rUser;
}
Except for patron.relation, the rest of the extraction works.
Questions:
How should I extract value of user.patron.relation?
Is there a more succint way of writing the above?
Thanks!
How should I extract value of user.patron.relation?
You would use { … patron: {relation: rUser.relation}, … } as the destructuring target.
Is there a more succint way of writing the above?
If the property names don't match there's not much you can do, but in your particular case you can simplify by destructuring the parameter into multiple variables:
const rUsers = children.map(({firstName, lastName, patron: {relation}}) =>
({firstName, lastName, relation})
);
You can use object destructuring like this:
const {firstName, lastName, patron: {relation}} = user
And relation has been set to the value of user.patron.relation.
You can return the object you want like so:
return {firstName, lastName, relation}
I'm seeing a few things wrong with your code.
patron.relation -> patron isn't defined, and can't be used as a key.
In the second example, rUser is used - I think you just want user.
My understanding of what you're trying to do essentially create a clone? If so, you should be able to do something like this.
let rUsers = children.map(user => {
firstName: user.firstName,
lastName: user.lastName,
relation: user.relation
});
Alternatively, it could be simpler to use Object.assign()
let rUsers = children.map(user => Object.assign({}, user));

Using Mojolicious: access a perl hash, passed through an ajax call as json, in javascript [duplicate]

Using DBIx::Class, I found a solution to my issue, thankfully. But I'm sure there has to be a nicer way.
my $record = $schema->resultset("food")->create({name=>"bacon"});
How would I turn this record into a simple hashref instead of having to make this call right after.
my record = $schema->resultset("food")->search({name=>"bacon"})->hashref_array();
Ideally I want to be able to write a code snippet as simple as
{record=> $record}
instead of
{record => {name => $record->name, $record->food_id, ...}}
This would drive me insane with a table that has alot more columns.
I assume you're talking about DBIx::Class?
my $record = $schema->resultset("food")->create({name=>"bacon"});
my %record_columns = $record->get_columns;
# or, to get a HashRef directly
my $cols = { $record->get_columns };
# or, as you've asked for
my $foo = { record => { $record->get_columns } };
What you're looking for is included in DBIx::Class as DBIx::Class::ResultClass::HashRefInflator.

Functional test: how to set JSON data in a POST method?

I want to create a functional test for an action that receives a POST method with data in JSON format.
This is what I have:
info('set car')->
post('/user/'.$user->getId().'/set-car/'.$car->getId()'->
with('request')->ifFormat('json')->begin()->
isParameter('module', 'myModule')->
isParameter('action', 'myAction')->
end()->
But..where should I set the receiving json data?
sf 1.4
Regards
Javi
Did you checked out the page from Jobeet tutorial?
The first example is
$browser = new sfBrowser();
$browser->
get('/')->
click('Design')->
get('/category/programming?page=2')->
get('/category/programming', array('page' => 2))->
post('search', array('keywords' => 'php'))
;
It may be that I didn't understand you question at all.
Correct me if I'm wrong.
More of a workaround...
If you get the content in your action like this:
protected function getContent() {
$content = $this->request->getParameter('content');
if(!$content) $content = $this->request->getContent();
return $content;
}
public function executeIndex(sfWebRequest $request) {
$content = $this->getContent();
//do something with your content :D
}
that should allow you to test what you want by passing
post('search', array('content' => 'whatever my content is'));