Parent method implementation calls

an article added by: Sonja Lande at 05312007



In: Categories » » AJAX » Parent method implementation calls

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 notice

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.

Useful tools and features

Parent method implementation calls  
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. 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...

2. 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...

3. 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 ...

4. 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...

5. Coding Using Conventions and Not Configurations
Coding Using Conventions and Not Configurations Problem You want to make your JavaScript constructs more efficient by applying the Rails “convention over configuration” principle to them. Theory You may already be familiar with the programming platform Ruby on Rails, which is used to build Web applications. The focus of this recipe is not Ruby on Rails, but one aspect of Ruby on Rails namely, convention over configuration (see http://en.wikipedia.org/wiki/ Ruby_on_Rails for m...

6. Advantage of parameterless functions in JavaScript
Using Parameterless Functions Problem You want to take advantage of parameterless functions in JavaScript. Theory JavaScript functions for the most part have parameters. You may think that the previous sentence states the obvious after all, without parameters, what data could be passed to a function? JavaScript has the ability to declare functions that have no parameters, even though the caller of the function has passed parameters to the function. For example, let’s look at...

7. JavaScripot Functions
Treating Functions Like Objects Problem You want to take advantage of the fact that functions are objects (remember, everything is an object in JavaScript). Theory Many people think that a function is some keyword used in JavaScript. A function is also an object that can be manipulated. Knowing that a function is an object makes it very interesting from the perspective of writing JavaScript code, because the code can treat the function like another other object. This mean...