Parent method implementation calls

an article added by: Sonja Lande at 05312007


In: Root » » AJAX » Parent method implementation calls

French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic

The parent method implementation calls proxyFunc.apply and passes as parameters this and the modified arguments array args. Then when the function associated with the proxyFunc variable is executed, whenever this is referenced, the correct object instance is referenced. Using apply is extremely useful and simplifies the proxy method implementation. The downside is that the original method passed to the proxy method must also use apply. If when calling the original method the proxy function does not use apply, then errors might be generated.

When executed, the generated code will work properly. The challenge lies in the generation of the code. One approach is to generate a text buffer that represents the complete implementation of the proxy method. Another approach is to dynamically generate and evaluate some parts of the generated code, and use a dynamic function for the other parts. Each of these solutions is problematic, and the best solution is to use JavaScript generics. Using JavaScript generics, you can define the desired code, and at runtime the behavioral aspects can be expanded. The following code is the complete proxy generation solution.

   Source: /website/ROOT/scripts/jaxson/common.js
   proxy : function(instance, funcIdentifier,  newFunc) {
   if (!instance[funcIdentifier]) {
   throw new Error("Method does not exist on  this
   object (" + funcIdentifier +  ")");
   }
   var proxyPrototype = function() {
   var origFunc = __orig;
   var proxyFunc = __proxy;
   var args = new Array();
   for (var c1 = 0; c1 < arguments.length; c1  ++) {
   args.push(arguments[0]);
   }
   args.push(origFunc);
   args.push(arguments);
   return proxyFunc.apply(this, args);
   }
   instance[funcIdentifier] = Generics.expand(proxyPrototype,
   {
   __orig: instance[funcIdentifier],
   __proxy: newFunc
   });
 },

The proxy implementation does not need much explanation, because the code has already been largely explained. Look at the definition of the proxyPrototype variable, and you should notice a similarity to the ideal generated code that was explained throughout the article. The only difference with proxyPrototype and the ideal code is that the values for origFunc and proxyFunc have not been defined.

The origFunc and proxyFunc variables have been assigned the __orig and __proxy variable values, which are identifiers used in the JavaScript generics expansion. The call to Generics.expand will expand the buffer to resemble the ideal buffer. The parameters to the proxy method are instance, which represents the object instance to generate a proxy method implementation; funcIdentifier, which represents a string buffer of the method to proxy; and newFunc, which represents the proxy function that is called before the original.

   The proxy method can be used as shown in the  following example.
   Source: /website/ROOT/ajax articles/javascript/proxy.html
   automated_proxy : function() {
   var cls = new DefinedClass();
   info("automated_proxy{cls.defined|before}",  cls.defined.toString());
   ops.proxy(cls, "defined",  function(tstValue, toCall, args) {
   info("proxyfunction", "parameter  (" + tstValue + ") received arg count (" +
   arguments.length + ") original arg count  (" + args.length + ")");
   toCall.apply(this, args);
   });
   info("automated_proxy{cls.defined|after}",  cls.defined.toString());
   cls.value = 10;
   cls.defined(10);
 }

In the implementation, the DefinedClass type is instantiated and assigned to the cls variable. For purposes of illustration, the implementation of cls.defined is generated (info(..., cls.defined.toString)) to show what the original method implementation is. The call to ops.proxy has three parameters, and the first two parameters (cls and "defined") are obvious. The third parameter is the proxy method, and in the case of the example, it is an inline-defined function. The third parameter does not have to be an inline method; it can be a function reference or another class instance. After the call to ops.proxy is another call to the info function to illustrate the modified version of the cls.defined method.

Tip To test your own understanding, run the test code from the samples to see how the code works. The last line of code that calls the defined method illustrates that the proxy method is called before the original method. When implementing the Proxy pattern for a method, keep the following points in mind:

• The Proxy pattern for a method is applied when you want to filter, preprocess, and postprocess parameters before (if) they are passed to the method.

• The implementation of the proxy method is responsible for calling the original method.

• The Proxy pattern for a method is best implemented by embedding functions within functions. The advantage of using an embedded approach is that the function can be referenced by other object instances without corrupting the method implementation.

• The Proxy pattern for a method can be embedded multiple times.

legal disclaimer

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

related articles

1. Understanding the Definition and Philosophy of Ajax
The focus of this article is to provide solutions to some common, general problems and questions that are bound to arise before or during development of Asynchronous JavaScript and XML (Ajax) and Representational State Transfer (REST) applications. These common questions are not always technical in nature, often leaning more toward theory or philosophy of development. The problem with these kinds of questions is that once you begin to think about them, you keep going in a circle and end up where you star...

2. Understanding the Definition and Philosophy of Web Services and SOA
Understanding the Definition and Philosophy of Web Services and SOA Wikipedia offers the following definition of Web services:4 The W3C defines aWeb service as a software system designed to support interoperable machine-to-machine interaction over a network. This definition encompasses many different systems, but in common usage the term refers to those services that use SOAPformatted XML envelopes and have their interfaces described by WSDL. For ex...

3. Understanding the Definition and Philosophy of REST
Understanding the Definition and Philosophy of REST REST is a controversial topic among Web service enthusiasts, because it’s considered to stand for the opposite of what Web services and SOA are trying to achieve. The problem with this thinking is that REST is not in contradiction with the abstract definition of SOA and Web services. REST is in contradiction with technologies such as SOAP, WSDL, and WS-* specifications. The following offers a quick definition of REST:...

4. The Easiest Way to Get Started with Ajax and REST
The Easiest Way to Get Started with Ajax and REST Problem You want to know the best way to get started with writing Ajax and REST. Solution When developing an Ajax and REST application, you must decide on the tools and frameworks you’ll use. The choice is simple: Use whatever you’re using today, and write some Ajax applications. You don’t need to change the tools you’re using today. Whether you’re using ASP.NET, JavaServer Pages (JSP), PHP, Ruby, or Python, you...

5. Testing a Dynamic Contract with Ajax
Coding the Contract Using Test-Driven Development Techniques Coding the contract using agile and test-driven development techniques requires writing a number of tests and implementing aMock URL layer. Problem You want to code the contract using these development techniques. Solution To demonstrate, let’s define a use case, implement the use case as a contract, write a test case(s) to implement the contract, implement the contract in the Mock URL, and finally...

6. Testing the Client Side Logic
Problem You want to effectively test your application’s client-side logic. Theory Testing GUI code tends not to be a productive task because of the complications that arise. The main complication is how to test the correctness of a user interface. Imagine a situation where clicking a button causes a table to be filled with data. Now imagine that when a check box is checked and the button is clicked again, a different table is filled with content. The fact that clicking the same button results in two ...

7. Understanding JavaScript and Types
Understanding JavaScript and Types Problem You want to work around the fact that JavaScript does not have types declared for its variables. Theory JavaScript code does not have any variables with a declared type. The lack of typed variables is apparent when you declare functions. That said, not having typed variable declarations does not mean JavaScript has no types or no type safety. Let’s start out with the simple declaration of a function, as illustrated by the following ex...