DirectMethods: passing collection of objects to javascript

  1. #1

    DirectMethods: passing collection of objects to javascript

    Thank you for your time and guidance.

    I have the following customer class as follow,

        public class Customer
        {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    
        [DirectMethod]
        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { ID = "1", FirstName = "Jim", LastName = "Jones" });
            customers.Add(new Customer { ID = "2", FirstName = "Joe", LastName = "Adams" });
            customers.Add(new Customer { ID = "3", FirstName = "Jake", LastName = "Johnson" });
            return customers;
        }
    How do we pass this collection of customers to a javascript function?

    Thank you for your time!
  2. #2
    The #5 sample in the following demo is similar to your requirements:

    https://examples2.ext.net/#/Events/D...hods/Overview/
    Geoffrey McGill
    Founder
  3. #3
    Quote Originally Posted by geoffrey.mcgill View Post
    The #5 sample in the following demo is similar to your requirements:

    https://examples2.ext.net/#/Events/D...hods/Overview/
    Thank you for your suggestion!

    I have been testing around with the above tutorial.

    Is it possible to assign the return value of a DirectMethod to a javascript variable?

    a sample code would be,

    var customerID =    App.direct.GetCustomer({
                    success: function (customer) {
                        //Ext.Msg.alert('Customer', customer.ID);
                        return customer.ID;
                    }
                });

    Here is a complete code for reference:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="myEXT.WebForm7" %>
    <%@ Import Namespace="Ext.Net.Utilities" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script runat="server">
            // Define Customer Class
            public class Customer
            {
                public int ID { get; set; }
                public string FirstName { get; set; }
                public string LastName { get; set; }
                public string Company { get; set; }
                public Country Country { get; set; }
                public bool Premium { get; set; }
            }
    
            // Define Country Class
            public class Country
            {
                public string Name { get; set; }
            }
    
            [DirectMethod]
            public static Customer GetCustomer()
            {
                // Get your Customer data from somewhere...
    
                return new Customer() { 
                    ID = 99,
                    FirstName = "Peter",
                    LastName = "Smith",
                    Company = "CompanyX, LLC.",
                    Premium = true,
                    Country = new Country { Name = "Canada" }
                };
            }
        </script>
    
        <script type="text/javascript">
    
            function init() {
            var customerID =    App.direct.GetCustomer({
                    success: function (customer) {
                        //Ext.Msg.alert('Customer', customer.ID);
                        return customer.ID;
                    }
                });
            }
        </script>
    
    
    
    </head>
    <body>
     <ext:ResourceManager runat="server"></ext:ResourceManager>
        <ext:Button ID="Button1" runat="server" Text="Click Me" Icon="Lightning">
        <Listeners>
            <Click Handler="init();" />
        </Listeners>
        </ext:Button>
    
    </body>
    </html>
  4. #4
    Hi @oooh,

    It calls a success handler asynchronously. It means that by the time of success handler's execution, a JavaScript function which calls a DirectMethod is executed already. So, inside a success handler you cannot deal with variables defined inside a JavaScript function which calls a DirectMethod. But you can apply it to some global variable.

Similar Threads

  1. App / DirectMethods Objects gone?
    By Bubu in forum 1.x Help
    Replies: 2
    Last Post: Oct 31, 2012, 12:31 AM
  2. Replies: 2
    Last Post: Jul 10, 2012, 1:23 PM
  3. [CLOSED] Referencing page controls in JavaScript objects
    By PLoch in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Aug 16, 2011, 5:59 PM
  4. Call DirectMethods from Javascript
    By ginsar in forum 1.x Help
    Replies: 4
    Last Post: Jun 08, 2011, 11:59 AM
  5. Replies: 1
    Last Post: Dec 22, 2009, 10:08 AM

Posting Permissions