Categories
discuss

How to transform a WebService call that is using behaviours?

We have some really old code that calls WebServices using behaviours (webservice.htc), and we are having some strange problems… since they’ve been deprecated a long time ago, I want to change the call.

What’s the correct way of doing it? It’s ASP.NET 1.1

Answer

You should be able to generate a proxy class using wsdl.exe. Then just use the web service as you normally would.

Categories
discuss

Best practices with jQuery form binding code in an application

We have an application with a good amount of jQuery JSON calls to server side code. Because of this, we have a large amount of binding code to parse responses and bind the appropriate values to the form. This is a two part question.

  1. What is the reccomended approach for dealing with a large number of forms that all have different data. Right now were are trying to take a structured approach in setting up a js “class” for each page, with an init, wireClickEvents etc.. to try to have everything conformed.

  2. Is there any “best practices” with creating repetitive jQuery code or any type of reccomended structure other than just throwing a bunch of functions in a js file?

Answer

Not 100% sure example what you are asking, but personally, and I use MochiKit, I create JavaScript “classes” (or widgets, if you prefer) for every significant client-side UI structure. These know, of course, how to populate themselves with data.

I don’t know what more there is to say – writing UI code for the browser in JavaScript is no different than writing UI code for other types of apps, as far as I am concerned. Build classes and instantiate them as needed, populate them with data, have them throw events, etc. etc.

Am I up in the night on this? 🙂


EDIT: In other words, yes – do what you are doing, for the most part. I see too many novice JavaScript hackers write a bunch of poorly-cohesive functions that don’t appear to be a part of anything specific other than they are all in a single file. Hope that makes sense.

Categories
discuss

What more is needed for Ajax than this function

I have a small JS function that does Ajax for me and another like it that adds in POST data to the request. With Ajax being such a big topic with so many libraries about it, what am I missing from my function, is it insecure or something else worrying?

function loadPage(pagePath, displayElement)
{
    var xmlHttp;

    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById(displayElement).innerHTML = xmlHttp.responseText;
        }
    }

    xmlHttp.open("GET", pagePath, true);
    xmlHttp.send(null);
}

Answer

I strongly recommend you not roll your own Ajax code. Instead, use a framework such as Prototype, Dojo, or any of the others. They’ve taken care of handling all the ReadyStates you’re not handling (2 means it’s been sent, 3 means it’s in process, etc.), and they should escape the reponse you’re getting so you don’t insert potentially insecure javascript or something into your page.

Another thing a more robust framework will give you is the ability to do more than just use innerHTML to replace items in the DOM. Your function here can only be used to replace one element with the response from the ajax call. There’s a lot more you can do with Ajax.

Categories
discuss

What is the best way to add an event in JavaScript?

I see 2 main ways to set events in JavaScript:

  1. Add an event directly inside the tag like this:

    <a href="" onclick="doFoo()">do foo</a>

  2. Set them by JavaScript like this:

    <a id="bar" href="">do bar</a>

and add an event in a <script> section inside the <head> section or in an external JavaScript file, like that if you’re using prototypeJS:

Event.observe(window, 'load', function() {
    $('bar').observe('click', doBar);
}

I think the first method is easier to read and maintain (because the JavaScript action is directly bound to the link) but it’s not so clean (because users can click on the link even if the page is not fully loaded, which may cause JavaScript errors in some cases).

The second method is cleaner (actions are added when the page is fully loaded) but it’s more difficult to know that an action is linked to the tag.

Which method is the best?

A killer answer will be fully appreciated!

Answer

In my experience, there are two major points to this:

1) The most important thing is to be consistent. I don’t think either of the two methods is necessarily easier to read, as long as you stick to it. I only get confused when both methods are used in a project (or even worse on the same page) because then I have to start searching for the calls and don’t immediately know where to look.

2) The second kind, i.e. Event.observe() has advantages when the same or a very similar action is taken on multiple events because this becomes obvious when all those calls are in the same place. Also, as Konrad pointed out, in some cases this can be handled with a single call.

Categories
discuss

My (Java/Swing) MouseListener isn’t listening, help me figure out why

So I’ve got a JPanel implementing MouseListener and MouseMotionListener:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DisplayArea extends JPanel implements MouseListener, MouseMotionListener  {
    public DisplayArea(Rectangle bounds, Display display) {
        setLayout(null);
        setBounds(bounds);
        setOpaque(false);
        setPreferredSize(new Dimension(bounds.width, bounds.height));

        this.display = display;
    }

    public void paintComponent(Graphics g) {
         Graphics2D g2 = (Graphics2D)g;
         if (display.getControlPanel().Antialiasing()) {
             g2.addRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
         }
         g2.setColor(Color.white);
         g2.fillRect(0, 0, getWidth(), getHeight());
    }

    public void mousePressed(MouseEvent event) {
        System.out.println("mousePressed()");
        mx1 = event.getX();
        my1 = event.getY();
    }

    public void mouseReleased(MouseEvent event) {
        System.out.println("mouseReleased()");
        mx2 = event.getX();
        my2 = event.getY();

        int mode = display.getControlPanel().Mode();
        switch (mode) {
        case ControlPanel.LINE:
             System.out.println("Line from " + mx1 + ", " + my1 + " to " + mx2 + ", " + my2 + ".");
        }
    }

    public void mouseEntered(MouseEvent event) {
        System.out.println("mouseEntered()");
    }

    public void mouseExited(MouseEvent event) {
        System.out.println("mouseExited()");
    }

    public void mouseClicked(MouseEvent event) {
        System.out.println("mouseClicked()");
    }

    public void mouseMoved(MouseEvent event) {
        System.out.println("mouseMoved()");
    }

    public void mouseDragged(MouseEvent event) {
         System.out.println("mouseDragged()");
    }

    private Display display = null;

    private int mx1 = -1;
    private int my1 = -1;
    private int mx2 = -1;
    private int my2 = -1;
}

The trouble is, none of these mouse functions are ever called. DisplayArea is created like this:

da = new DisplayArea(new Rectangle(CONTROL_WIDTH, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT), this);

I am not really a Java programmer (this is part of an assignment), but I can’t see anything glaringly obvious. Can someone smarter than I see anything?

Answer

The implements mouselistener, mousemotionlistener just allows the displayArea class to listen to some, to be defined, Swing component’s mouse events. You have to explicitly define what it should be listening at. So I suppose you could add something like this to the constructor:

this.addMouseListener(this);
this.addMouseMotionListener(this);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..