[CLOSED] .ToScript() for short-version definition fragment?

  1. #1

    [CLOSED] .ToScript() for short-version definition fragment?

    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"}
    Last edited by fabricio.murta; Sep 06, 2016 at 6:45 PM.
  2. #2
    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!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    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!
    Fabrício Murta
    Developer & Support Expert
  4. #4
    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.
    Last edited by sveins12; Sep 02, 2016 at 8:29 PM.
  5. #5
    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?
    Fabrício Murta
    Developer & Support Expert
  6. #6
    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.
    Last edited by sveins12; Sep 02, 2016 at 11:31 PM.
  7. #7
    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?..
    Fabrício Murta
    Developer & Support Expert
  8. #8
    I think the anonymous object approach will solve the case. Thank you!
    Last edited by sveins12; Sep 04, 2016 at 12:48 AM.
  9. #9
    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.
    Fabrício Murta
    Developer & Support Expert
  10. #10
    ClientConfig.Serialize is exactly what I was looking for:

    var json = new ClientConfig().Serialize(model.ToComponent(), true);
    Thanks
    Last edited by sveins12; Sep 06, 2016 at 10:41 AM.

Similar Threads

  1. [CLOSED] Use Short Cut to open window in browser
    By jesperhp in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Jul 12, 2013, 7:05 AM
  2. Winow.ToBuilder().ToScript()
    By arit in forum 1.x Help
    Replies: 1
    Last Post: Jun 24, 2011, 1:32 PM
  3. [CLOSED] ToScript() requires HttpContext
    By randy85253 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: May 10, 2010, 4:50 AM
  4. [CLOSED] Bug with .ToScript() for ComponentMenuItem
    By pil0t in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 05, 2010, 4:25 PM
  5. displaying date in short format in a grid
    By pearl in forum 1.x Help
    Replies: 2
    Last Post: Dec 19, 2009, 3:14 AM

Posting Permissions