Sunday, September 14, 2014

Simple application using ExtJs 5.0

First I would like to tell that I am new to ExtJs development. So this is not the best solution for this kind of requirement. I have tried this example as an learning task.

Building a contact form as shown in the image.


Requirements:
1. Include the fields as in the image.
2.Contact purpose should be [General, Sales, Support]
3.Client side should be in ExtJS, ASP.NET MVC4/5, and server side should be ASP.NET Web API
4.Client side code should be based on Sencha CMD generated application.
5.Upon successful submit, user should get a message from the server. “Your message have been received”. Show it using a MessageBox to user.
6.All fields are required and email should be validated on client side.

1. First I have installed ExtJs with the help of the following link.


Now I have it in my system PATH variable. So I can run and compile using command prompt.

Then create the relevant files using the following command and you can see the relevant generated files in the location you have given.

sencha generate app -ext Assingment D:/Sencha/SenchaProjects/Assingment



2. Then create ASP.NET Web API project in Visual Studio and copy the content of above assignment folder into the project you have created in VS.


3. To make run in the browser the generated application using Sencha CMD as it is, do the following changes.
i. Copy and paste the following code in your Index.cshtml located in View/Home folder.

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>ProjectManagement</title>
    <script id="microloader" type="text/javascript" src="bootstrap.js"></script>
    
   @* <script type="text/javascript" src="~/app.js"></script>*@
   
</head>
    <body class="body">
    </body>
</html>

ii. To enable jason MIME type, copy and paste the following change in your global Web.config file.

<staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json" />
 </staticContent>

Now you see the generated application above in your browser.

4. Then do the necessary changes to make your task to get completed.
4.1 . I have created my model as Contact.js 
Ext.define('Assingment.model.Contact', {
    extend'Ext.data.Model',
    alias'model',
    fields: [
        { name'Name' },
        { name'Company' },
        'Email',
        'ContactPurpose',
        'Subject',
        { name'Description' }
    ],
 
    schema: {
        namespace'MyApp.model',
        urlPrefix'/Home/SubmitInformation',
        proxy: {
            api: {
                read'{prefix}/{entityName:uncapitalize}',
            },
            reader: {
                type'json',
                sucess'model.success'
            },
            extraParams: {
                name' '
            }
        }
    }
});

4.2. view/main/Main.js is like this.

var namestore = new Ext.data.SimpleStore({ fields: ['contactPurpose'], data: [['General'],['Sales'],['Support']] });
 
Ext.define('Assingment.view.main.Main', {
    extend'Ext.form.Panel',
 
    
    xtype'app-main',
    controller'main',
    viewModel: {
        type'main'
    },
 
 
    title'Contact Form',
    height: 1000,
    width: 1000,
    bodyPadding: 10,
    layout'form',
 
    items:[
        {
            xtype'form',
            reference'fooForm',
            bind'{main}',
            items:
            [
                {
                    fieldLabel'Name',
                    name'name',
                    xtype'textfield',
                    bind'{Name}'
                },
                {
                    fieldLabel'Company',
                    name'company',
                    xtype'textfield',
                    bind'{Company}'
                },
                {
                    fieldLabel'Email',
                    name'email',
                    vtype:'email',
                    fieldStyle'background-color: #F7FE2E; background-image: none;',
                    displayField"email",
                    xtype'textfield',
                    bind'{Email}'
                },
                {
                    xtype'combobox',
                    fieldLabel'Contact Purpose',
                    displayField'contactPurpose',
                    name"contactPurpose",
                    storenamestore,
                    bind'{ContactPurpose}'
                },
                {
                    fieldLabel'Subject',
                    name'subject',
                    displayField"subject",
                    fieldStyle'background-color: #F7FE2E; background-image: none;',
                    xtype'textfield',
                    bind'{Subject}',
                    width: 450
                },
                {
                    xtype'textareafield',
                    growfalse,
                    width: 450,
                    bodyPadding: 10,
                    name'description',
                    fieldLabel'Description',
                    autoScrolltrue,
                    bind'{Description}'
                }
            ],
            buttons: [
                {
                    text'Submit',
                    formBindtrue,
                    handler'onClickButton',
                }]
        }]   
});
4.3. view/main/MainCOntroller.js is like this where I call the action method in Home controller as url: /Home/SubmitInformation with POST request.

Ext.define('Assingment.view.main.MainController', {
    extend'Ext.app.ViewController',
    requires: [
        'Assingment.model.Contact'
    ],
    alias'controller.main',


    onClickButtonfunction() {
        var form = this.lookupReference('fooForm');
 
        if (form.isValid()) {
            url:
                form.submit({
                    url'/Home/SubmitInformation',
                    method'POST',
                    successfunction (form, action) {
                        var data = Ext.JSON.decode(action.response.responseText);
                        Ext.Msg.alert('Success', data.data);
                    },
                    failurefunction(form, action) {
                        Ext.Msg.alert('Failed', action.result ? action.result.msg : 'No response');
                    },
                    params:
                    {
                        view'sencha',
                        jsontrue
                    }
                });
        }
    },

    onConfirmfunction (choice) {
        if (choice === 'yes') {
            //
        }
    }
});


4.4.view/main/MainModel.js where 'ContactInformation' is located in root Models/ContactInformation.cs

Ext.define('Assingment.view.main.MainModel', {
    extend'Ext.app.ViewModel',

    alias'viewmodel.main',

    data: {
        name'Assingment'
    },
    stores: {
        model'ContactInformation'
    },
});
4.5 ContactInformation.cs contains follows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace Assingment.Models
{
  public class ContactInformation
  {
    public string Name { getset; }
    public string Company { getset; }
    public string ContactPurpose { getset; }
    public string Description { getset; }
    public string Subject { getset; }
    public string Email { getset; }
 
    public ContactInformation()
    {}
 
    public ContactInformation(string Name, string Company, string ContactPurpose,string Description, string Subject, string Email)
    {
      this.Name = Name;
      this.Company = Company;
      this.ContactPurpose = ContactPurpose;
      this.Description = Description;
      this.Subject = Subject;
      this.Email = Email;
    }
  }
}
4.6. Root Controllers/HomeControllers.cs contains the action method like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
using Assingment.Models;
 
using Newtonsoft.Json;
 
namespace Assingment.Controllers
{
  public class HomeController : Controller
  {
    public ActionResult Index()
    {
      return View();
    }
 
    [HttpPost]
    [AllowAnonymous]
    public ActionResult SubmitInformation(ContactInformation col)
    {
 
      return this.JsonResponse(
        new
        {
          success = true,
          data = "Your message have been received",
 
        });
    }
 
    protected ContentResult JsonResponse(object response)
    {
      return new ContentResult { Content = JsonConvert.SerializeObject(response), ContentType = "application/json"ContentEncoding = System.Text.Encoding.UTF8 };
    }
 
  }
}
Following is my folder structure in VS.
That's it. Now you can run in VS and see the form in the browser.
This is the output.
Hope you this will help for you. :) You can try improve this example.

8 comments:

  1. Please contact me at eriveraa@gmail.com. I have some questions to you. Thanks.

    ReplyDelete
  2. Hi Nalani, could you please make this same example with extjs 4.x , asp.net mvc 5 and visual studio 2013?. It will help me a lot. Thanks in advance.

    ReplyDelete
  3. Hi Lalo, I will try the example and publish with mvc 5 and VS 2013 with ExtJs 5 when I get the time. It might not have much difference with extjs 4.x.

    ReplyDelete
  4. Hi Nalani, thanks for your response. Can we talk by email or skype please?

    ReplyDelete
  5. Sure. I will reply for anything related to this post. But I am quite new to the ExtJs subject. :) I may not be able to help much.

    ReplyDelete
  6. I would prefer your email or skype account so we can talk directly. If you don't mind :)

    ReplyDelete
  7. Its nalani.jayasundara@gmail.com .

    ReplyDelete
  8. Hi Nalani, i have added you to my contacts on gmail. Please accept my request for chat on gmail :)

    ReplyDelete