1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.JSGeneration
.DynamicDispatching
20 /// Abstract class that contains the shared logic of JS Generation, separated from
21 /// the various view engines implementations
23 public abstract class JSGeneratorDispatcherBase
: DynamicDispatcher
25 private readonly IJSCodeGenerator codeGen
;
26 private readonly IJSGenerator generator
;
27 private readonly object[] elementExtensions
;
30 /// Initializes a new instance of the <see cref="JSGeneratorDispatcherBase"/> class.
32 /// <param name="codeGen">The code gen.</param>
33 /// <param name="generator">The generator.</param>
34 /// <param name="extensions">The extensions.</param>
35 /// <param name="elementExtensions">The element extensions.</param>
36 protected JSGeneratorDispatcherBase(IJSCodeGenerator codeGen
, IJSGenerator generator
, object[] extensions
, object[] elementExtensions
)
37 : base(generator
, extensions
)
39 this.codeGen
= codeGen
;
40 this.generator
= generator
;
41 this.elementExtensions
= elementExtensions
;
45 /// Gets the javascript code generator.
47 /// <value>The code gen.</value>
48 public IJSCodeGenerator CodeGen
50 get { return codeGen; }
54 /// Executes an operation (totally late bound)
56 /// <param name="method">The method.</param>
57 /// <param name="args">The args.</param>
58 /// <returns></returns>
59 protected object InternalInvoke(string method
, params object[] args
)
63 AssertValidElementAccessor(args
);
65 string root
= args
[0].ToString();
67 return CreateJSElementGeneratorProxy(codeGen
, generator
.CreateElementGenerator(root
), elementExtensions
);
70 if (HasMethod(method
))
72 return Dispatch(method
, args
);
75 return CreateNullGenerator();
79 /// Creates a null generator.
81 /// <returns></returns>
82 protected abstract object CreateNullGenerator();
85 /// Creates a JS element generator.
87 /// <param name="codeGen">The code gen.</param>
88 /// <param name="elementGenerator">The element generator.</param>
89 /// <param name="elementExtensions">The element extensions.</param>
90 /// <returns></returns>
91 protected abstract object CreateJSElementGeneratorProxy(IJSCodeGenerator codeGen
, IJSElementGenerator elementGenerator
, object[] elementExtensions
);
93 private static void AssertValidElementAccessor(object[] args
)
95 if (args
== null || args
.Length
!= 1)
97 throw new ArgumentException("el() method must be invoked with the element name as an argument");
101 throw new ArgumentException("el() method invoked with a null argument");