PDA

View Full Version : [CLOSED] .ToScript() for short-version definition fragment?



sveins12
Aug 14, 2016, 8:35 PM
I would like to serialize the definition of a Model-object into json. Now, please see the following code:



@{
var x = Html.X();
var model = x.Model()
.IDProperty("Foo")
.Fields(
x.ModelField().Name("Foo")
);
var json = model.ToScript();
}
@json


That code will return something like this:


Ext.ClassManager.isCreated(Ext.id()) ? Ext.id() : Ext.define(Ext.id(), {extend: "Ext.data.Model", fields:[{name:"Foo"}],idProperty:"Foo" });


Is there a built-in method/way which can return only the short version instead? Like this:


{fields:[{name:"Foo"}],idProperty:"Foo"}

fabricio.murta
Aug 16, 2016, 6:35 AM
Hello! I didn't really look into your question in detail, but by your question, our classes have the .ToScript() and also .ToConfig() to translate, respectively, the code into a runnable script or object-definition of the component.

I hope this helps. If not, just let us know!

fabricio.murta
Sep 02, 2016, 9:06 PM
Hello! Didn't hear back from you for some days now... Wondering if the above helped you or not. Let us know if you still need assistance in this topic!

sveins12
Sep 02, 2016, 9:27 PM
Hi again. I found that those two methods do the same thing. They return a long "stand-alone" version of the script-fragment needed to define an Extjs-component (with ID, class-type and everything). None of them does the thing I was thinking about. You know Extjs object definitions have a short form in addition to that long form, where the container decides what type it is and so on. I was looking for a way to get such a short javascript definition for a component. I am not sure if Ext.NET has a method for this. Anyway, in my case I needed the js-definition for a model-object (for a store), and I created it manually instead.

The reason is that I want to send the datamodel together with the data in some cases, because in mobile apps they might have old version of the data-model in their device. I am just doing a lot of experiments.

But I will come back to this as I go deeper into the Ext.Mobile. I am waiting for a newer beta version or a release.

fabricio.murta
Sep 02, 2016, 9:44 PM
Hello!

I believe that, to get that "condensed object" form, all you have to do is serialize the object using JSon. Maybe the object's .Serialize() method is what you need?

sveins12
Sep 03, 2016, 12:22 AM
I think I tried to serialize with json serializers, but maybe not the right one. They all try to serialize the C#-version of the component, and not like a javascript-one. They raise exceptions like "A circular reference was detected while serializing an object of type 'System.Globalization.CultureInfo'."

I think I need to use the Ext.NET-serializer, but I don't know how.

fabricio.murta
Sep 04, 2016, 1:41 AM
Hello @sveins12,

Just to clarify, you could get the format you want if you just make an anonymous object and serialize it, but you wanted to know whether there was a way to directly serialize the Model object into a simple string as you exemplified in your first post, right?..

sveins12
Sep 04, 2016, 1:45 AM
I think the anonymous object approach will solve the case. Thank you!

fabricio.murta
Sep 06, 2016, 4:10 AM
Hello! Glad using anonymous objects work for you, I guess that would be the easiest way to do so.

But if you still want a way to turn Ext.NET objects into simple text representation, what you can do is call ClientConfig's Serialize method (in this case) specifying it to ignore any custom serialization settings.

This won't work to a Razor Syntax builder object instance though, you'd have to use Ext.Net.Model instead of Html.X().Model().

Here is how your code should look for that to work



@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<title>Index</title>
</head>
<body>
<div>
@Html.X().ResourceManager()

@{
var x = Html.X();

var model = new Ext.Net.Model() {
IDProperty = "Foo",
Fields = {
new ModelField()
{
Name = "Foo"
}
}
};

var json = new ClientConfig().Serialize(model, true);
}
@json
</div>
</body>
</html>


Now you can choose what is best for you. Probably anonymous object usage will be simpler and cleaner in this case.

sveins12
Sep 06, 2016, 11:28 AM
ClientConfig.Serialize is exactly what I was looking for:



var json = new ClientConfig().Serialize(model.ToComponent(), true);


Thanks