mercredi 27 juillet 2016

Hide DIVs by ID on page load

I have the following JS in the page layout (running RoR):

<script>
  $(document).ready(function() {
    document.getElementById('1').style.display = 'none';
    document.getElementById('2').style.display = 'none';
    document.getElementById('3').style.display = 'none';
    document.getElementById('4').style.display = 'none';
    document.getElementById('5').style.display = 'none';
    });
</script>

I am trying to have the 5 divs to be hidden on page load but can not seem to get it to work. I can use JS or JQuery. I have tried this as well:

$(window).load(function() {
  ...js code...
});

and still could not get it to hide the divs on page load.

What do I need to write in JS to have div with id 1..5 to be hidden?

N-ary Tree in C: How to add nodes and store data?

I want to create an n-ary tree composed in this way:

FileFolderTree

Each node has a key, name, type (File/Folder) and URL:

struct node {
    int key;
    char[10] name;
    char[100] url;
    char type;
} node;

Every node can have a different number of child nodes.

I already read about a solution that uses linked lists: N-ary trees in C
But it doesn't say anything about algorithms for adding or accessing nodes.

How can I store my data in a tree like this?
How can I add nodes to the linked list?
How can I access the data once the tree is built?

Passing semaphores as an argument to functions

I'm working on a static analysis tool which detects if a there's a mismatch for between lock/release calls for a semaphore. The detection is specific to VxWorks RTOS.

I came across this testcase and my tool detects this as mismatch between lock and release of semaphores because my implementation is solely based upon comparing the semaphore strings passed to the lock/release call.

void fun(char semid);
char id,i;
int main()
{
    id = semCreate();         //initializing a semaphore
    fun(id);
    semGive(id);              //semaphore release call
    return 0;
}
void fun(char semid)
{
    semTake(semid);          //semaphore lock call
    i++;        
}

Logically the code makes sense, but is this a correct way of using semaphores? Is this a regular programming practice or is it plainly invalid?

Some detailed code supporting or rejecting the usage of semaphores as given above would be highly appreciated.

ReactJS determine if component is on screen

I'm designing an application and am wondering whether React can handle the use case. I'm going to have a canvas that has boxes. Inside those boxes will be input fields, buttons, spinners, etc. There will be hundreds of boxes. The user will be able to zoom in/out, as well as pan in any direction, such that the user can zoom in and a single box will fill the entire page, and zoom out so that the user can see all boxes. I'd like to be able to detect whether any portion of an arbitrary box is visible, so that I don't spend time updating the fields with constantly changing server information if the box isn't visible to the user. Is this something that can be accomplished with React?

Thanks in advance for any help. I apologize if my terminology is incorrect, and it probably is.

Symfony Event Listener

Hi I'm tring to do a Symfony event listener following this documentation: http://symfony.com/doc/2.8/cookbook/doctrine/event_listeners_subscribers.html

<?php

namespace FMAppBundleEventListener;

use DoctrineORMEventLifecycleEventArgs;
use FMAdminBundleEntityAddressBillingAddress;

class BillingAdressListener
{
    /**
     * @param LifecycleEventArgs $args
     */
    public function listenBillingAdress(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        if(!$entity instanceof BillingAddress){
            return;
        }

        $this->postPersist($args);
    }

    /**
     * @param LifecycleEventArgs $args
     */
    public function postPersist(LifecycleEventArgs $args)
    {
        $em = $args->getEntityManager();
        $billingAdress = $args->getEntity();

        dump($billingAdress); die();
    }
}

service.yml

billing_adress.listener:
        class: FMAppBundleEventListenerBillingAdressListener
        tags:
            - { name: doctrine.event_listener, event: listenBillingAdress }

But nothing is happening when I'm submitting a form with the BillingAddress object.

Did I do something wrong?

Laravel error handling when route does not exist

I have a route to show my users' profile in my laravel project. But when you go to an url and fill in a username that does not exist it gives a nasty error, obviously because that username doesn't exist in the database.

Has anyone any idea how I can error handle this?

Here's my route:

Route::get('user/{name}', 'userController@showUser');

Here's my function:

public function showUser($name)
    {
        $user = User::where('name' , '=', $name)->firstOrFail();
        return view('user.show', compact('user'));
    }

This is what I've tried but doesn't seem to work since I get this error: View not found

$user = User::where('name' , '=', $name)->first();
        if(!empty($user)){
            return view('user.show', compact('user','projects'));
        }else{
            return view('user');
        }

Prevent user from pressing backspace

I'm developing a program that uses malloc and realloc functions to increment the pointer buffer in real-time while the user is typing a string.

The problem is, that I'd like to prevent the user from hitting Backspace to correct the input. Is it possible to block Blackspace key somehow in C while using getche()?

My final program will have two inputs: one without Backspace (you can't go back), and another with Backspace. (you can correct the input and then press Enter).

char *szString;
char *tmp;
int i = 0;
char c;

szString = '�';
szString = malloc(1);

printf("Enter a string: ");

while ((c = getche()) != 'r')
{
    if(c = 0x08) // BackSpace
    {
        //
    }
    szString[i] = c;
    i++;
    tmp = realloc(szString, i+1);
    szString = tmp;
}

szString[i] = '�';

printf("nYou typed: %s", szString);