Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Monday, March 26, 2012

How do I create static functions in JavaScript?

According to the docs online, the Error class has static functions

I can do the following
var err = Error.create(message, errorInfo);

without having to create a new object first. The documentation online does not explain how to create your own static functions using the MS approach.

Any ideas how I can create my own static functions?

Andre

try this:

var StaticClass =
{
//Static Variables
varInt : 0,
varString : '',
varArray : [],

StaticMethod1 : function()
{
//Accessing the Vars
StaticClass.varInt = 5;
},

StaticMethod2 : function()
{
//Calling another static method
StaticClass.StaticMethod1();
},
}


//Outside the Class:

StaticClass.StaticMethod1()

Hope this help.


When I use this approach, it seems that I can not register the class with ScriptManager anymore.
I have been digging around in MS Ajax code to see how MS creates static classes like the Error class.

Here is a snippet:

Error.__typeName = 'Error';
Error.__class = true;

Error.create = function Error$create(message, errorInfo) {

}

...

I tried to use this approach but it did not work.

Also I would be able to use a namespace instead of only a class name.


Yes you will be able to add it in ScriptManager, but won't be able to use registerClass. Unless you are not creating any component, behavior or control there is no need to use registerClass. To include namespace, add your name as you did for regular class and acess theclass with namespace.

How do I create a WebService with mandatory elements in the request?

I need a bit of assitance creating a WebService. Say that I have something like this -

'The objectsPublic Class productPublic nameAs StringPublic priceAs DecimalEnd ClassPublic Class orderPublic customerAs StringPublic productsAs List(Of product)End Class'The DB layerPublic orderDBPublic Function commitOrder(orderAs order)As IntegerDim orderIdAs Integer'DB code ommittedReturn orderIdEnd FunctionEnd Class'The Web Service<System.Web.Services.WebService(Namespace:="http://localhost/")> _<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ToolboxItem(False)> _Public Class order_wsInherits System.Web.Services.WebService <WebMethod()> _Public Function commitOrder(ByVal orderAs order)As Integer Dim orderDBAs New orderDB()Return orderDB.commitOrder(order)End FunctionEnd Class
The WSDL I get contains something like -

<s:element name="commitOrder"><s:complexType><s:sequence><s:element minOccurs="0" maxOccurs="1" name="order" type="tns:order" /></s:sequence></s:complexType></s:element><s:complexType name="order"><s:sequence><s:element minOccurs="0" maxOccurs="1" name="customer" type="s:string" /><s:element minOccurs="0" maxOccurs="1" name="products" type="tns:ArrayOfproducts" /></s:sequence></s:complexType><s:complexType name="ArrayOfproducts"><s:sequence><s:element minOccurs="0" maxOccurs="unbounded" name="product" nillable="true" type="tns:product" /></s:sequence></s:complexType><s:complexType name="product"><s:sequence><s:element minOccurs="0" maxOccurs="1" name="name" type="s:string" /><s:element minOccurs="0" maxOccurs="1" name="price" type="s:decimal" /></s:sequence></s:complexType>

Notice that everything is minoccurs="0" and the product entry in the ArrayOfproducts is nillable. How do I make it so that specific bits are required (minoccurs="1" and/or product element not nillable)?

Thanks

Martin

You could set a web service header so they always send you those requiered fields.


I'm not sure that I understand what you mean. But, just to elaborate on what I am looking for, the WSDL should contain tags that specify minOccurs="1".


Might be better to use discrete parameters rather than sending an order object. Most people find that more straightforward to use.


How would you control mandatory / optional elements using discrete parameters?

In any case, I'm already severely restricted in my attempts to use best coding practices by the inadequacies of .Net's WebService implementation. If I can't even control mandatory elements in this scenario I'll give it a miss and look at using Java and Mule.