[CLOSED] [1.0] Store and REST

  1. #1

    [CLOSED] [1.0] Store and REST

    Hello,

    If you could try the below example I'm receiving an unexpected response from the create action.

    ExampleController.cs:
        public class ExampleController : System.Web.Mvc.Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult Create()
            {
                var result = new AjaxStoreResult
                {
                    ResponseFormat = StoreResponseFormat.Save
                };
    
                result.SaveResponse.Success = true;
    
                return result;
            }
    
            public ActionResult Update()
            {
                var result = new AjaxStoreResult
                {
                    ResponseFormat = StoreResponseFormat.Save
                };
    
                result.SaveResponse.Success = true;
    
                return result;
            }
    
            public ActionResult GetData()
            {
                IList result = new List<object>();
    
                result.Add(new
                {
                    Id = 1,
                    Name = "Timothy"
                } );
    
                result.Add(new
                {
                    Id = 2,
                    Name = "Geoffrey"
                } );
    
                return new AjaxStoreResult
                {
                    ResponseFormat = StoreResponseFormat.Load,
                    Data = result,
                    Total = result.Count
                };
            }
        }
    Index.aspx:
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    <!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>Example</title>
    </head>
    <body>
        <ext:ResourceManager ID="ResourceManager1" runat="server" ScriptMode="Debug" />
        <ext:GridPanel ID="GridPanel1" runat="server"
            AutoExpandColumn="Name"
            Height="250"
            Layout="fit">
            <Store>
                <ext:Store ID="Store1" runat="server"
                    AutoLoad="true"
                    RemotePaging="true"
                    RemoteSort="true"
                    Restful="true">
                    <Proxy>
                        <ext:HttpProxy Json="true">
                            <RestAPI
                                Create="/Example/Create"
                                Destroy="/Example/Delete"
                                Read="/Example/GetData"
                                Update="/Example/Update" />
                        </ext:HttpProxy>
                    </Proxy>
                    <Listeners>
                        <Exception Handler="
                            Ext.net.Notification.show({
                                iconCls    : 'icon-exclamation', 
                                html       : e &amp;&amp; e.message ? e.message : response.message || response.statusText, 
                                title      : 'EXCEPTION', 
                                hideDelay  : 5000
                            });" />
                        <Save Handler=" Ext.net.Notification.show({
                                iconCls    : 'icon-information', 
                                html       : arg.message, 
                                title      : 'Success', 
                                hideDelay  : 5000
                            });" />
                    </Listeners>
                    <Reader>
                        <ext:JsonReader IDProperty="Id" Root="data" SuccessProperty="success">
                            <Fields>
                                <ext:RecordField Name="Id" />
                                <ext:RecordField Name="Name" SortDir="ASC" />
                            </Fields>
                        </ext:JsonReader>
                    </Reader>
                </ext:Store>
            </Store>
            <BottomBar>
                <ext:Toolbar runat="server">
                    <Items>
                        <ext:Button runat="server" Text="Add vladimir">
                            <Listeners>
                                <Click Handler="#{GridPanel1}.insertRecord(0, { Name : 'vladimir' } ); #{GridPanel1}.save();" />
                            </Listeners>
                        </ext:Button>
                    </Items>
                </ext:Toolbar>
            </BottomBar>
            <ColumnModel>
                <Columns>
                    <ext:Column ColumnID="Name" DataIndex="Name" Header="Name">
                        <Editor>
                            <ext:TextField runat="server" />
                        </Editor>
                    </ext:Column>
                </Columns>
            </ColumnModel>
            <Plugins>
                <ext:RowEditor runat="server" ClicksToEdit="2" SaveText="Update">
                    <Listeners>
                        <AfterEdit Handler="#{GridPanel1}.save();" />
                    </Listeners>
                </ext:RowEditor>
            </Plugins>
        </ext:GridPanel>
    </body>
    </html>
    Replication steps:

    1. Load page
    2. Click Add vladimir
    3. Notice it throws an exception in the bottom right corner?

    Update works with the same method. Can you help? Would really like to get this working soon!

    Cheers,
    Timothy
  2. #2

    RE: [CLOSED] [1.0] Store and REST

    Hi,

    You have to use RestResult
    See http://forums.ext.net/showthread.php?postid=23531.aspx
  3. #3

    RE: [CLOSED] [1.0] Store and REST

    Awesome vladimir, shame on me for not seeing that ... soo much code to look through :)

    Cheers
  4. #4

    RE: [CLOSED] [1.0] Store and REST

    vladimir,

    When I run the Create action what should I return? Can you give an example?


    Cheers,
    Timothy
  5. #5

    RE: [CLOSED] [1.0] Store and REST

    Hi,

    See RestDemo view and its controller
    Here is snippet from controller
    [AcceptVerbs(HttpVerbs.Post)]
            public RestResult Create(string data)
            {
                try
                {
                    Category c = JSON.Deserialize<Category>(data);
                
                    NorthwindDataContext context = this.DBContext;
                    context.Categories.InsertOnSubmit(c);
                    context.SubmitChanges();
    
                    return new RestResult
                               {
                                   Success = true,
                                   Message = "New category added",
                                   Data = new {c.CategoryID} // we have to return new generated ID
                               };
                }
                catch (Exception e)
                {
                    return new RestResult
                    {
                        Success = false,
                        Message = e.Message
                    };
                }
            }
  6. #6

    RE: [CLOSED] [1.0] Store and REST

    Thanks vladimir,

    What about returning a Total value for the read API when we are using paging on our GridPanel?


    Cheers,
    Timothy
  7. #7

    RE: [CLOSED] [1.0] Store and REST

    Hi,

    Data can be any object, you can include any information (including total), just unsure that store will read that information automatically. How are you planning to use that total field?
  8. #8

    RE: [CLOSED] [1.0] Store and REST

    Hello vladimir,

    I'm using the Total when returning a list of objects back to the store on read:

    public ActionResult GetDepartments(int limit, int start, string dir, string sort)
    {
        var filter = new DataFilter
        {
            Limit = limit,
            Start = start,
            SortColumn = sort,
            SortDirection = dir
        };
    
        var result = DepartmentService.GetDepartments(filter);
    
        return new RestResult
        {
            Data = result,
            Total = filter.Total
        };
    }
    Using the total to calculate the paging :)

    Cheers
  9. #9

    RE: [CLOSED] [1.0] Store and REST

    Hi Timothy,

    You can use Json result instead RestResult. Just pass object to the JsonResult which contains success, message, data properties. Also you can add Total property if it is required

Similar Threads

  1. [CLOSED] Rest api.
    By farisqadadeh in forum 1.x Legacy Premium Help
    Replies: 15
    Last Post: Aug 19, 2011, 11:22 AM
  2. [CLOSED] REST with WCF - part 2
    By daneel in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Apr 14, 2011, 2:40 PM
  3. [CLOSED] REST with WCF and Ext.NET 1.0
    By daneel in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 11, 2011, 10:34 AM
  4. [CLOSED] EventStore and REST API
    By craig2005 in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Mar 04, 2011, 3:19 PM
  5. [CLOSED] [1.0] REST question
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 12
    Last Post: Nov 30, 2009, 12:48 PM

Posting Permissions