New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A possibility to emit a regular class instance as an object literal (only properties) #177

Closed
DaniilVeriga opened this Issue Apr 14, 2015 · 5 comments

Comments

Projects
None yet
3 participants
@DaniilVeriga
Contributor

DaniilVeriga commented Apr 14, 2015

The feature request came from this forum thread:
http://forums.bridge.net/forum/community/help/161

@DaniilVeriga DaniilVeriga added this to the 1.3 milestone Apr 14, 2015

@mac2000

This comment has been minimized.

Show comment
Hide comment
@mac2000

mac2000 Apr 14, 2015

It will be nice if there will be something like ko.toJS() in Bridge namespace

But still it is yet another call that we should remember to do

At least for this moment maby be there is a way to modify jQuery post, ajax, getJSON etc implementations so they will check is given data object instance of class and if so call Bridge.ToJS

mac2000 commented Apr 14, 2015

It will be nice if there will be something like ko.toJS() in Bridge namespace

But still it is yet another call that we should remember to do

At least for this moment maby be there is a way to modify jQuery post, ajax, getJSON etc implementations so they will check is given data object instance of class and if so call Bridge.ToJS

@mac2000

This comment has been minimized.

Show comment
Hide comment
@mac2000

mac2000 Apr 15, 2015

Also there is same situation when retrieving responses from ajax call.

Suppose we have generic handler like this:

public class SampleHandler : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.Write(JsonConvert.SerializeObject(new Response { Success = true, Message = "OK" }));
    }
}

which returns instance of:

public class Response
{
    public bool Success { get; set; }
    public string Message { get; set; }
}

in my ajax call I assume that I can unbox it like this:

jQuery.Post("/SampleHandler.ashx", new {}, (object obj, string str, jqXHR request) => {
    var model = (Response)obj; // raises: Uncaught Unable to cast type Object to type Core.Response
    Console.Log(model);
}, "json");

mac2000 commented Apr 15, 2015

Also there is same situation when retrieving responses from ajax call.

Suppose we have generic handler like this:

public class SampleHandler : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.Write(JsonConvert.SerializeObject(new Response { Success = true, Message = "OK" }));
    }
}

which returns instance of:

public class Response
{
    public bool Success { get; set; }
    public string Message { get; set; }
}

in my ajax call I assume that I can unbox it like this:

jQuery.Post("/SampleHandler.ashx", new {}, (object obj, string str, jqXHR request) => {
    var model = (Response)obj; // raises: Uncaught Unable to cast type Object to type Core.Response
    Console.Log(model);
}, "json");
@DaniilVeriga

This comment has been minimized.

Show comment
Hide comment
@DaniilVeriga

DaniilVeriga Apr 15, 2015

Contributor

Thank you for all the suggestions. Yeah, it is something we are thinking on as well.

As for (Response)obj. It is rather an opposite operation. The issue is about converting a class instance to a literal, but (Response)obj is opposite - converting a literal to a class instance. There is a possible solution. Please start a new forum thread and we'll discuss in greater details.

Contributor

DaniilVeriga commented Apr 15, 2015

Thank you for all the suggestions. Yeah, it is something we are thinking on as well.

As for (Response)obj. It is rather an opposite operation. The issue is about converting a class instance to a literal, but (Response)obj is opposite - converting a literal to a class instance. There is a possible solution. Please start a new forum thread and we'll discuss in greater details.

@geoffreymcgill geoffreymcgill removed this from the 1.3 milestone Apr 17, 2015

@DaniilVeriga

This comment has been minimized.

Show comment
Hide comment
@DaniilVeriga

DaniilVeriga Dec 8, 2015

Contributor

JSON.Stringify(person) and JSON.Parse(JSON.Stringify(person)) appear to be a solution.

I am leaving the issue open to monitor further if there is still a dedicated need in a specific method on a class instance.

Contributor

DaniilVeriga commented Dec 8, 2015

JSON.Stringify(person) and JSON.Parse(JSON.Stringify(person)) appear to be a solution.

I am leaving the issue open to monitor further if there is still a dedicated need in a specific method on a class instance.

@geoffreymcgill

This comment has been minimized.

Show comment
Hide comment
@geoffreymcgill

geoffreymcgill Sep 8, 2017

Member

Seems like there are many options now to solve this issue. Using ObjectLiteral is one, and directly calling Bridge.toPlain() is another.

Adding the [ObjectLiteral] attribute may be an option too.

Example

https://deck.net/73bf185d06d1ecb4b85e785cd6bc0338

public class Program
{
    public static void Main()
    {
        var p = new Person();
        
        p.Name = "Frank";
        
        var obj = new ObjectLiteral(p);
        
        Console.WriteLine(obj);
        
        // Directly call Bridge.toPlain
        //@ obj = Bridge.toPlain(p);
        
        Console.WriteLine(obj);
    }
}

public class Person
{
    public string Name { get; set; }
}
Member

geoffreymcgill commented Sep 8, 2017

Seems like there are many options now to solve this issue. Using ObjectLiteral is one, and directly calling Bridge.toPlain() is another.

Adding the [ObjectLiteral] attribute may be an option too.

Example

https://deck.net/73bf185d06d1ecb4b85e785cd6bc0338

public class Program
{
    public static void Main()
    {
        var p = new Person();
        
        p.Name = "Frank";
        
        var obj = new ObjectLiteral(p);
        
        Console.WriteLine(obj);
        
        // Directly call Bridge.toPlain
        //@ obj = Bridge.toPlain(p);
        
        Console.WriteLine(obj);
    }
}

public class Person
{
    public string Name { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment