Stomp - multiple subscriptions each with a unique handler - comet

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.

Related

Angular: Routing between pages using condition

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']);
}
}

Linq to SQL - Turn off UpdateCheck in code

I am wanting to turn off the UpdateCheck functionality for all members (except their primary keys). Now I was following the example below as guidance, however my MetaDataMembers of the table are still set to Always.
http://www.the-lazy-coder.com/2013/04/set-updatecheck-to-never.html
The above code snippet just gets you to change the attribute, however it seems to never get picked up, as I can debug the code when it is running and I see all the properties being set, so I am presuming that the attributes changing does not change the underlying object.
Now if I were to change approach and just get the MetaDataMembers directly from the RowType I notice they have the UpdateCheck property, however only a getter. So is there a way to (via reflection if needed) overwrite this property once it is set? Even after looking at decompiled source it is an abstract class and I cannot find any implementations to use for reference.
I am using SQLMetal to generate the Context files, so there is no designer tinkering available, and although some people will say that I should run some text editing macros to parse and change the attributes, it all sounds too long winded when I should just be able to go into the object in memory and tell it to ignore whatever it has been told previously.
SO! Is there a way to override the property in the entities? I have tried running the original code in that link in both constructor, after the objects created and just before I am about to do an update, however none of the changes seem to stick or at least propagate to where it matters, and there is hardly any material on how to do any of this progmatically.
After searching around the internet I found no nice way to do it, and although there is the link I mentioned originally it doesn't work as it works on the attributes which are partly right but in the case above they are working on the attributes which are not in memory and are just the decorations, anyway the code below seems to work but is not nice:
public static void SetUpdateCheckStatus(this IDataContext dataContext, UpdateCheck updateCheckStatus)
{
var tables = dataContext.Mapping.GetTables();
foreach (var table in tables)
{
var dataMembers = table.RowType.DataMembers;
foreach (var dataMember in dataMembers)
{
if (!dataMember.IsPrimaryKey)
{
var dataMemberType = dataMember.GetType();
if (dataMemberType.Name == "AttributedMetaDataMember")
{
var underlyingAttributeField = dataMember.GetType().GetField("attrColumn", BindingFlags.Instance | BindingFlags.NonPublic);
if (underlyingAttributeField != null)
{
var underlyingAttribute = underlyingAttributeField.GetValue(dataMember) as ColumnAttribute;
if (underlyingAttribute != null)
{ underlyingAttribute.UpdateCheck = updateCheckStatus; }
}
}
else
{
var underlyingField = dataMember.Type.GetField("updateCheck", BindingFlags.Instance | BindingFlags.NonPublic);
if (underlyingField != null)
{ underlyingField.SetValue(dataMember, updateCheckStatus); }
}
}
}
}
}
The IDataContext is just a wrapper we put around a DataContext for mocking purposes, so feel free to change that to just DataContext. It is written extremely defensively as this way pulls back lots of members which do not have all the desired data so it has to filter them out and only work on the ones which do.

Scala.swing GUI -- How to separate multiple reaction cases

I am currently in the process of turning a sudoku solving program into a GUI with scala.swing and running into some trouble with the use of different functions. That is to say, I have a function for completely solving the puzzle, another for offering a hint entry, and another that will reset the grid. The interface consists of 81 individual ComboBox'es (see: http://i.imgur.com/45vzpei.png) and three buttons that perform said functions. My problem is that, while the separate reactions/cases involved reference specifically which buttons/functions to listen to, any button will incite all of the functions. My code for each of the listeners/buttons looks something like the following
listenTo(solve,comb11,comb12,comb13,comb14,comb15,comb16,comb17,comb18,comb19,comb21,comb22,comb23,comb24,comb25,comb26,comb27,comb28,comb29,comb31,comb32,comb33,comb34,comb35,comb36,comb37,comb38,comb39,comb41,comb42,comb43,comb44,comb45,comb46,comb47,comb48,comb49,comb51,comb52,comb53,comb54,comb55,comb56,comb57,comb58,comb59,comb61,comb62,comb63,comb64,comb65,comb66,comb67,comb68,comb69,comb71,comb72,comb73,comb74,comb75,comb76,comb77,comb78,comb79,comb81,comb82,comb83,comb84,comb85,comb86,comb87,comb88,comb89,comb91,comb92,comb93,comb94,comb95,comb96,comb97,comb98,comb99)
reactions += {
case ButtonClicked(solve) =>
...[working code for solve function]...
}
(The 'comb##'s are the exhaustive 81 ComboBoxes and the 'solve' is the button that solves the whole puzzle.) If I get rid of all but one of the listener/reaction blocks of code, clicking the remaining button works perfectly. If I try to include two or all of the listener/reaction code blocks, then every button causes ALL functions to be performed, which is clearly confusing and undesirable.
Not sure I understand your problem. But if you use lower case names in pattern matching extraction, these are fresh variables, and have nothing to do with values of the same name defined elsewhere. So to react to the solve button, you need to match against the value solve which you can do by putting it in back ticks:
listenTo(allMyButtons: _*)
reactions += {
case ButtonClicked(`solve`) => // note the back ticks!
...[working code for solve function]...
}
Otherwise, why don't you just keep each reaction with each combo box?
val combos = Vector.tabulate(81) { i =>
new ComboBox(1 to 9) {
listenTo(this)
reactions += {
case ButtonClicked(_) =>
... // not important to check the button - we only listen to one!
}
}
}
There is also a shorter way of defining the reaction to a pressed button.
import swing.{MainFrame, FlowPanel, Button}
val frame = new MainFrame {
contents = new FlowPanel {
contents += Button("solve")(println("solve"))
}
visible = true
}

code igniter dynamic routing

I am using code igniter.
What I want to do is,
if a page is visited that does not exist
example.com/idontexist
Then I want to first check a database to see if idontexist is in the database.
If it is then I want to route the user to
example.com/action/idontexist.
How can I do this?
I feel like this gets asked every week.
Open up your application/config/routes.php, then add something like this:
$route['^(:any)'] = "my_controller/get_article/$1";
Please note that it will route everything to a controller called action. If you have other controllers then you should add a route for them too (preferably placed before this one).
// EDIT: Using this you can goto http://your-site.com/secrets-of-internet-marketing and it will call the get_article function in the my_controller controller, and pass "secrets-of-internet-marketing" as the first argument. Which can then process with something like this:
public function get_article($article_name) {
// something like this:
$article = $this->article_model->get_model_by_name($article_name);
$this->load->view('article', $article);
}
One solution would be to extend the CI_Router class with your own in application/core/MY_Router.php and just copy the method _set_routing() in your class.
Your changes would go somewhere after routes.php file is included and set to $this->routes property, you can add your custom routes.
include(APPPATH.'config/routes'.EXT);
...
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
unset($route);
//Get your custom routes:
$your_routes = $this->_get_custom_routes();
foreach($your_routes as $custom_route)
{
$this->routes[$custom_route['your_regex_match']] = $custom_route['controller'].'/'.$custom_route['action'];
}
Of course you might not need this, but I hope it gives you some idea.
This way you can add custom routes and since you will have to fetch them from database, consider caching them for better speed.

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) {
}