ajax - mkol/il2js GitHub Wiki
Easy and transparent synchronous (explicit and implicit) and asynchronous calls to server, just call method. Serialization, deserialisation, calling AJAX (Asynchronous JavaScript and XML) methods will be done automatically. IL2JS uses mostly JSON for transporting data in AJAX calls.
[RunAt(RunAt.Server)]
public static string F(string x) {
return x+x;
}
public void Page_Load() {
Alert(F("abc")); // implicity RPC call
Alert(Server.Call<string,string>(F,"abc")); // explicit RPC call
Server.AsyncCall<string,string>(F,"abc",x=>{Alert(x);}); // asynchrouns explicit RPC call with handler (what on error?)
}
IL2JS supports serialization of following types:
- primitive type (int,double,etc.)
- string
- IDictionary<string,T>
- IEnumerable
- T[]
- class with fields of T type
where T denotes any of types listed here.
Only serialization of tree-based structures is supported (e.g. serialization of cyclic structure will result in runtime error).
In case of acyclic structures, which have multiple references to the same objects serializer will create multiple copies of it.
// In fact, this attribute can be omited here, but if present - IL2JS uses optimalizations.
[DataContract]
public class Data {
// Only fields marked with this attribute will be serialized.
// All parameters of the attribute are ignored.
[DataMember]
public string Name;
}
[RunAt(RunAt.Server)]
public static Data F(Data x) {
return new Data {
Name = x.Name+x.Name
};
}
public void Page_Load() {
var data = new Data { Name="abc" };
Alert(F(data).Name);
Alert(Server.Call<Data,Data>(F,data).Name);
Server.AsyncCall<Data,Data>(F,data,x=>{Alert(x.Name);});
}
[DataContract]
public class Data {
[DataMember]
public string Name;
}
[RunAt(RunAt.Server)]
public static Data F(Data x) {
return new Data {
Name = x.Name+x.Name
};
}
[RunAt(RunAt.ClientOrServer)]
public static Data F2(Data x) {
return new Data {
Name = x.Name+x.Name
};
}
public static Data F3(Data x) {
return new Data {
Name = x.Name+x.Name
};
}
[RunAt(RunAt.Client)]
public static Data F4(Data x) {
return new Data {
Name = x.Name+x.Name
};
}
public void Page_Load() {
var data = new Data { Name="abc" };
Alert(F(data).Name); // this will call F3 on Server. There will be no code for it on Client side.
Alert(Server.Call<Data,Data>(F,data).Name);
Server.AsyncCall<Data,Data>(F,data,x=>{Alert(x.Name);});
Alert(F2(data).Name); // this will call F2 on Client
Alert(Server.Call<Data,Data>(F2,data).Name); // this on Server
Server.AsyncCall<Data,Data>(F2,data,x=>{Alert(x.Name);}); // this also on Server
Alert(F3(data).Name); // this will call F3 on Client
Alert(Server.Call<Data,Data>(F3,data).Name); // this will result in error
Server.AsyncCall<Data,Data>(F3,data,x=>{Alert(x.Name);}); // this also result in error
Alert(F4(data).Name); // this will call F4 on Client
Alert(Server.Call<Data,Data>(F4,data).Name); // this will result in error
Server.AsyncCall<Data,Data>(F4,data,x=>{Alert(x.Name);}); // this also result in error
}