Techaray

Shailendra Saxena's Technical Blog

Javascript Function2

clock March 13, 2016 19:26 by author ShailendraSaxena

document.writeln('Hello, world!');

//object declaration in javascript 1.

var stooge = {

    "first-name": "Jerome",

    "last-name": "Howard"

};

 

//object declaration in javascript 2.

var flight = {

    airline: "Oceanic",

    number: 815,

    departure: {

        IATA: "SYD",

        time: "2004-09-22 14:55",

        city: "Sydney"

    },

    arrival: {

        IATA: "LAX",

        time: "2004-09-23 10:42",

        city: "Los Angeles"

    }

};

 

document.writeln(stooge["first-name"]);

document.writeln(flight.airline);

 

stooge['first-name'] = 'Jerome22';

 

document.writeln(stooge["first-name"]);

 

var x = stooge;

x.nickname = 'Curly';

document.writeln(stooge["nickname"]);

stooge.profession = 'actor';

 

document.writeln('number' + '  ' + typeof flight.number);

 

var another_stooge = Object.create(stooge);

another_stooge['first-name'] = 'Harry';

another_stooge['middle-name'] = 'Moses';

another_stooge.nickname = 'Moe';

stooge.profession = 'actor';

delete another_stooge.nickname;

var name;

for (name in another_stooge) {

    if (typeof another_stooge[name] !== 'function') {

        document.writeln(name + ': ' + another_stooge[name]);

    }

}

//Global Abatement

//JavaScript makes it easy to define global variables that can hold all of the assets of

//your application. Unfortunately, global variables weaken the resiliency of programs

//and should be avoided.

//One way to minimize the use of global variables is to create a single global variable

//for your application:

var MYAPP = {};

 

 

//That variable then becomes the container for your application:

 

MYAPP.stooge = {

"first-name": "Joe",

"last-name": "Howard"

};

MYAPP.flight = {

    airline: "Oceanic",

    number: 815,

    departure: {

    IATA: "SYD",

    time: "2004-09-22 14:55",

    city: "Sydney"

},

    arrival: {

    IATA: "LAX",

    time: "2004-09-23 10:42",

    city: "Los Angeles"

}

};

 

//Function in Javascript.

//A function encloses a set of statements. Functions are the fundamental modular unit

//of JavaScript. They are used for code reuse, information hiding, and composition.

//Functions are used to specify the behavior of objects. Generally, the craft of programming

//is the factoring of a set of requirements into a set of functions and data

//structures.

 

var add = function (a, b) {

    return a + b;

};

 

//Function Objects

//Functions in JavaScript are objects. Objects are collections of name/value pairs having

//a hidden link to a prototype object. Objects produced from object literals are

//linked to Object.prototype. Function objects are linked to Function.prototype

//(which is itself linked to Object.prototype). Every function is also created with two

//additional hidden properties: the function’s context and the code that implements

//the function’s behavior.

 

 

 

//The Method Invocation Pattern

//When a function is stored as a property of an object, we call it a method. When a

//method is invoked, this is bound to that object. If an invocation expression contains

//a refinement (that is, a . dot expression or [subscript] expression), it is     invoked as a method:

    // Create myObject. It has a value and an increment

    // method. The increment method takes an optional

    // parameter. If the argument is not a number, then 1

    // is used as the default.

        var myObject = {

            value: 0,

            increment: function (inc) {

                this.value += typeof inc === 'number' ? inc : 1;

            }

        };

    myObject.increment( );

    document.writeln(myObject.value); // 1

    myObject.increment(1);

    document.writeln(myObject.value); // 1

    myObject.increment(2);

    document.writeln(myObject.value); // 3

 

 

    (function () {

        var a = b = 3;

    })();

 

    document.writeln("a defined? " + (typeof a !== 'undefined'));

    document.writeln("b defined? " + (typeof b !== 'undefined'));

 

 

    function foo1() {

        return { bar: "hello"};

    }

 

    function foo2() {

        return

        {

            bar11: "ahello"

        };

    }

 

    document.writeln("foo1 returns:");

    document.writeln(foo1());

    document.writeln("foo2 returns:");

    document.writeln(foo2());

 

function sumDigits(num) {

     var i, sum = 0;                  // can declare two variables at once

 

     for (i = 1; i <= num; i++) {

             sum += i;              // add each number to sum (ie, 1 + 2 + ...+ num)

     }

 

     // Display result

     alert("The sum of the digits from 1 to "+ num + " is:\n\n\t " + sum);

}

 

 

 



JavaScript in ASP.Net

clock March 13, 2016 19:20 by author ShailendraSaxena

ADD Function Like this in <head>of html.

<script type="text/JavaScript" language="JavaScript">
function ValidateForm()
{
    var ret = true;
    if (document.getElementById("txtFirstName").value == "")
    {
        document.getElementById("lblFirstName").innerText = "First Name is required";
        ret = false;
    }
    else
    {
        document.getElementById("lblFirstName").innerText = "";
    }
 
    if (document.getElementById("txtLastName").value == "")
    {
        document.getElementById("lblLastName").innerText = "Last Name is required";
        ret = false;
    }
    else
    {
        document.getElementById("lblLastName").innerText = "";
    }
 
    if (document.getElementById("txtEmail").value == "")
    {
        document.getElementById("lblEmail").innerText = "Email is required";
        ret = false;
    }
    else
    {
        document.getElementById("lblEmail").innerText = "";
    }
 
    return ret;
}
</script>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
    OnClientClick="return ValidateForm()" onclick="btnSubmit_Click" />




Dependency Injection with AutoFac 2

clock March 13, 2016 18:29 by author ShailendraSaxena

3. UserController

publicclassUserController : Controller

    {

 

        IUserRepository _userrepos;

 

        public UserController( IUserRepository _repository)

        {

            _userrepos = _repository;

        }

 

        // GET: User

        publicActionResult Index()

        {

            var data = _userrepos.GetUserList();

            return View(data);

        }

 

        // GET: User/Details/5

        publicActionResult Details(int id)

        {

            var data = _userrepos.GetUser(id);

            return View(data);

        }

 

        // GET: User/Create

        publicActionResult Create()

        {

            return View();

        }

 

        // POST: User/Create

        [HttpPost]

        publicActionResult Create(User user)

        {

            try

            {

                _userrepos.Add(user);

                _userrepos.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

 

            }

            return View();

        }

 

        // GET: User/Edit/5

        publicActionResult Edit(int id)

        {

            return View();

        }

 

        // POST: User/Edit/5

        [HttpPost]

        //public ActionResult Edit(int id, FormCollection collection)

        publicActionResult Edit(int id, User usr)

        {

            try

            {

                usr.UserId = id;

                // TODO: Add update logic here

                _userrepos.Update(usr);

                _userrepos.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

 

        // GET: User/Delete/5

        publicActionResult Delete(int id)

        {

            return View();

        }

 

        // POST: User/Delete/5

        [HttpPost]

        publicActionResult Delete(int id, FormCollection collection)

        {

            try

            {

                // TODO: Add delete logic here

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

    }

4. Autofac install (autofac + autofacMVC)

5. Global.asax.

  protectedvoid Application_Start()

        {

            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

            RouteConfig.RegisterRoutes(RouteTable.Routes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);

 

            var builder = newContainerBuilder();

            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            builder.RegisterType(typeof(UserRepository)).AsImplementedInterfaces();

            //builder.RegisterType(typeof(CustomerRepository)).AsImplementedInterfaces();

            var container = builder.Build();

            DependencyResolver.SetResolver(newAutofacDependencyResolver(container));

 

 

 

        }



Dependency Injection with AutoFac

clock March 13, 2016 18:27 by author ShailendraSaxena

 

1.       1. Interface

publicinterfaceIUserRepository

    {

        void Add(User user);

        void Update(User user);

        void Delete(int Id);

        User GetUser(int Id);

        IEnumerable<User> GetUserList();

        void SaveChanges();

    }

2. Repository

publicclassUserRepository : IUserRepository

    {

        readonlyDataContext _context;

        public UserRepository()

        {

            _context = newDataContext();

        }

        publicvoid Add(User user)

        {

            _context.User.Add(user);

            _context.SaveChanges();

        }

        publicvoid Delete(int Id)

        {

            User usr = _context.User.Find(Id);

            _context.User.Remove(usr);

        }

        publicUser GetUser(int Id)

        {

            User usr = _context.User.Find(Id);

            return usr;

        }

        publicIEnumerable<User> GetUserList()

        {

            return _context.User.ToList();

        }

        publicvoid SaveChanges()

        {

            _context.SaveChanges();

        }

        publicvoid Update(User user)

        {

            _context.Entry(user).State = EntityState.Modified;

        }

 



Search

Calendar

<<  August 2017  >>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

Archive

Tags

Categories


Visitors

FlagCounter

Flag Counter

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign In