Angular: Routing between pages using condition - html

I am trying to route between pages using basic if condition in Angular.
GoToHome() {
if(this.router.url=='/chat'){
console.log(this.router.url)
this.router.navigate(['login']);
} else {
this.router.navigate(['people']);
}
}
The problem is that the route chat isn't really correct, there are many pages in chat (chat\x , chat\y and many others) I want that it will work for all the pages in chat, but right now it doesn't work. If I write a specific route like chat\x it does work, but only for x. Is there a way to do it for all?

you can read and check Guards. Read about CanActivate method, maybe it will help you?

RouteGuards might do a better job of handling the redirects as per your requirement.
But a quick workaround would be to do a split() on the URL and compare for the chat part. Try the following
if(((this.router.url).split('/')[1]) === 'chat') {
// proceed
}

As other had said, best solution is to use Angular Guard https://medium.com/#ryanchenkie_40935/angular-authentication-using-route-guards-bf7a4ca13ae3.
Anyway to resolve your problem you can use startsWith() function which determines whether a string begins with the characters of a specified string.
GoToHome() {
if((this.router.url).startsWith('/chat'){
console.log(this.router.url)
this.router.navigate(['login']);
} else {
this.router.navigate(['people']);
}
}

Related

How to use querystring for wherebetween? Knex

I'm pretty sure this should be the best idea to solve the problem I'm facing right now,
using wherebetween().
But I'm not sure how I could use it.
I've gotta deal with a querystring ?from=2020-03-15T00%3A00%3A00.000Z&to=2020-03-20T00%3A00%3A00.000Z
To print the data between 3-15 and 3-20.
There is an and expression in the middle of from and to.
.modify(function(queryBuilder) {
if (req.query.industry) {
queryBuilder.where('industry',"like",`%${req.query.industry}%`);
}
})
This is the code that I've used for single querystring, but not sure how to work with it for multiple query strings.
.modify(function(queryBuilder) {
if (req.query.from && req.query.to) {
queryBuilder.whereBetween('timestamp',[`%${req.query.from}%`,`%${req.query.to}%`]);
}
})
this is what i have came up with so far, seems like its working but not printing anything...
seems like the problem is caused by the form the date is writte, is there any way I could use 2020-03-15T00%3A00%3A00.000Z as 2020-03-15T00:00:00Z
Does dropping those % symbols from query which are wildcards for like operator work?
queryBuilder.whereBetween('timestamp',[req.query.from, req.query.to]);

Returning functional HTML code through Angular JS filter

I'm trying to filter any text through angular to return a font-awesome icon. Instead It's just returning plain text. Anyone know a fix? Thanks!
.filter('textToIcon', function($sce) {
return function(text) {
if(text!==''){
return '<i class="fa fa-child"></i>';
}
}
This is "Strict Conceptual Escaping" (SCE) in action.
For more information on what it is and how it works, take a look at this answer.
In a few words, Angular tries to protect your users from untrusted content that could do bad things to them. If you know that the values are safe to be interpreted as HTML, there are two approaches you can take:
Import ngSanitize (it is available as a separate module) and add it as a dependency in your module (e.g. angular.module('myApp', ['ngSanitize'])). You are set (as long as you pass safe values to ngBindHtml) !
Use $sce to specify that certain stuff is explicitly trusted (by you) to be used as HTML:
.filter('textToIcon', function ($sce) {
return function (text) {
if (text) {
return $sce.trustAsHtml('');
}
};
})
The former approach is strongly recommended !

Filtering the iNotes Calendar in extlib

I need too filter the iNotes calendar control in extlib. When I look in the examples in the extlib application I can see that it is suppose to be connected to a xecalendarJsonLegacyService.
The problem I find with this service is that I can't filter the content based on category or search as with the other view services.
I need to create different calendars/json data based on a search or category in a view.
I have looked at some of the other services but not sure if it is possible to use them instead.
If you have any ideas for how I should create my filter, please respond.
I have attached pictures below showing both the jsonservice and the calendarcontrol.
This is what the json data look like in the xsCalendarJsonLegacyService
{
"#timestamp":"20120311T171603",
"#toplevelentries":"3",
"viewentry":
[
{
"#unid":"37F0330979C04AF2C12579BE004F5629",
"#noteid":"32E1A",
"#position":"1",
"#read":"true",
"#siblings":"3",
"entrydata":
[
{
"#columnnumber":"0",
"#name":"$134",
"datetime":
{
"0":"20120314T100000"
}
},
{
"#columnnumber":"1",
"#name":"$149",
"number":
{
"0":119
}
}, etc...
You could implement your own REST service (or extension to existing one) in an extension library, but I guess you are looking for something easier.
Sorry no code, but maybe (and hopefully) an answer.
Have you looked at the xc:CalendarStoreCustomRestService custom control inside the Xpages Extension Library demo? It looks like they connected the calendar control with a normal JSON view store and that supports search en keys.
I found code you could use but you will have to extend the custom control. I think it is a new component that is not yet included as a xe: component inside the Extension Library.
This is how you use the control:
<xc:CalendarStoreCustomRestService id="cc4ccCalendarStoreCustomRestService"
storeComponentId="notesCalendarStore1" databaseName="#{sessionScope.databaseName}"
viewName="($Calendar)">
</xc:CalendarStoreCustomRestService>
This is your calendar component, it uses the above storeComponentId.
<xe:calendarView id="calendarView1" jsId="cview1"
summarize="false"
type="#{javascript: null == viewScope.calendarType? 'M' : viewScope.calendarType }"
storeComponentId="notesCalendarStore1">
<xe:this.loaded><![CDATA[${javascript:if (sessionScope.databaseName == null) {
return false;
} else {
return true;
}}]]></xe:this.loaded>
</xe:calendarView>
If you need some more info, this example is included inside the DWA_iNotesRest.xsp.
I googled a long time and the only solution I`ve found is to build your own Rest service
have you managed to filter the Calendar without this?

Stomp - multiple subscriptions each with a unique handler

I am using Stomp / Orbited for Comet functionality.
In order to deal with multiple channels, I end up doing this:
stomp.onmessageframe = function(frame) {
if (frame.headers['destination'] == '/thisFeed/') {
//handle thisFeed
}
if (frame.headers['destination'] == '/thatFeed/') {
//handle thatFeed
}
....which is OK, I guess. But what if I don't know, at load time, how I want to handle a feed? I want to be able to do something like this:
stomp.subscribe('someOtherFeed', someOtherFeedHandler);
That way, when I subscribe, I can define the handler then and only then.
I have come up with one solution, but it's so very far from pretty.
When I create the stomp message, I add a "handler" property as a header, like so in python:
conn.send('Frank the Wonder Llama", destination="/infoAboutLlamas/", handler='llamas')
Then, in the javascript:
stomp.onmessageframe = function(frame) {
window[frame.headers['handler']]() //Execute the function named by the handler
}
...so then, the function llamas() gets called. I can then define (and redefine) llamas anywhere I want.
Now I'm sure this can't be the optimal solution. I do, on the other hand, like that it gives me a bit of flexibility to specify the handler I want to use right in python. But seriously, I'm thinking there is a better way.

how to skip undefined properties in JSON?

When I parse JSON fields coming from google maps, etc., it is a mess. Because they are not made specifically for my script I have to verify many details, epecially because the addresses are different in every country.
Short question: when the script finds a undefined property the script breaks...error..
How can I verify the property is defined?
if(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality != null) {
/***do something***/
}
Something like that doesn't seem to solve the problem. Why?
In JavaScript, accessing a property of an object that does not exist returns undefined, not null - heck, you said it in the title.
So, assuming that all the previous properties do actually exist, you can check that the Locality property exists using typeof, like this:
if(typeof (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.
Locality) !== 'undefined') {
/***do something***/
}
Or, (I think) you can use hasOwnProperty():
if (data.
Placemark[i].
AddressDetails.
Country.
AdministrativeArea.
SubAdministrativeArea.hasOwnProperty('Locality'))
{
/*** do something ***/
}
First, In javascript you can use "try / catch" like java or other programming language, this can let your code continue running if something goes wrong...
for your issue, you can test that :
if (typeof(data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality)
&&
data.Placemark[i].AddressDetails.Country
.AdministrativeArea.SubAdministrativeArea.Locality.length>0) {
}