Click here to Skip to main content
15,892,298 members
Articles / Web Development / HTML

A simple POC using ASP.NET Web API, Entity Framework, Autofac, Cross Domain Support

Rate me:
Please Sign up or sign in to vote.
4.92/5 (43 votes)
27 Oct 2012CPOL11 min read 340.1K   8.3K   154  
The purpose of this article is to create a proof of concept to demonstrate the feasibility of ASP.NET Web API Beta Version.
jQuery.support.cors = true;
var productcategoriesAddress = "http://localhost:8080/api/products/";

$(function () {
    $('#tblRight').hide();

    $.getJSON(
            productcategoriesAddress,
            function (data) {
                var parents = jQuery.grep(data, function (a) { return a.ParentProductCategoryID == null });
                var childs = jQuery.grep(data, function (a) { return a.ParentProductCategoryID != null });
                $.each(parents,
                    function (index, value) {
                        var categorydata = [];
                        var subCategory = '';
                        var subChild = jQuery.grep(childs, function (a) { return a.ParentProductCategoryID == value.ProductCategoryID });
                        $.each(subChild,
                            function (index, item) {
                                var serviceURL = productcategoriesAddress + item.ProductCategoryID;
                                subCategory = subCategory + "  " + "<a href=" + serviceURL + " class='menuButton'>" + item.Name + "</a>";
                            });
                        categorydata.push({
                            'ProductCategoryID': value.ProductCategoryID,
                            'ParentCategory': value.Name,
                            'ChildCategory': subCategory
                        });
                        $("#categoryTemplate").tmpl(categorydata).appendTo("#categories");
                        $("#" + value.Name).html(subCategory);
                    });

                    GetProducts(1);
            }
        );

    $("#Checkout").click(function () {
        $("#divMain").hide();
    });

    $(".menuButton").live("click", function () {
        $('#categories li h1').css('background', '#736F6E');
        $("#loader").show();
        $.ajax({
            type: "GET",
            datatype: "json",
            url: $(this).attr("href"),
            context: this,
            success: function (value) {
                $("#productData").html("");
                $("#productTemplate").tmpl(value).appendTo("#productData");
                $("#loader").hide();
            }
        });
        return false;
    });

    $(".AddToCart").live("click", function () {
        var selectedItem = $.tmplItem(this);
        var productQuantity = $("#txt" + selectedItem.data.ProductID).val();
        if (productQuantity == "" || productQuantity == null) {
            alert("Please select quantity for this product.")
            $("#txt" + selectedItem.data.ProductID).focus();
            return false;
        }
        else {
            if ($('#tblRight').hide())
                $('#tblRight').show();
            var totalPrice = parseFloat($("#lblCartTotal").html()) + (parseFloat(selectedItem.data.ListPrice) * parseFloat(productQuantity));
            $("#lblCartTotal").html(totalPrice.toFixed(2));
            var cartData = [];
            cartData.push({
                'ProductID': selectedItem.data.ProductID,
                'ProductCode': selectedItem.data.ProductNumber,
                'Quantity': productQuantity,
                'Price': (parseFloat(selectedItem.data.ListPrice) * parseFloat(productQuantity)).toFixed(2)
            });
            $("#CartProductTemplate").tmpl(cartData).appendTo("#cartData");
            $("#txt" + selectedItem.data.ProductID).val('');
        }
        return false;
    });

    $(".removeFromCart").live("click", function () {
        var currentPrice = parseFloat($(this).closest("tr").children()[2].innerHTML);
        $(this).closest("tr").remove();
        var totalPrice = parseFloat($("#lblCartTotal").html());
        $("#lblCartTotal").html(parseFloat(totalPrice - currentPrice).toFixed(2));
        if ($('#cartData').children().length == 0)
            $('#tblRight').hide();
    });

    $("body").addClass("ui-widget");
    $("input[type=\"submit\"], .menuButton").button()

});

function ChecOutItem() {
    var cartDataArray = new Array();

    $('#cartData tr').each(function () {
        var obj = this.cells[0].innerHTML + '|' + this.cells[2].innerHTML;
        cartDataArray.push(obj);
    });

    window.location.replace("checkout.htm?cart=" + cartDataArray);
}

function GetProducts(ProductCategoryID) {
    var serviceURL = productcategoriesAddress + ProductCategoryID;
    $('#categories li h1').css('background', '#736F6E');
    $('#mnu' + ProductCategoryID).css('background', '#357EC7');
    $("#loader").show();
    $.ajax({
        type: "GET",
        datatype: "json",
        url: serviceURL,
        context: this,
        success: function (value) {
            $("#productData").html("");
            $("#productTemplate").tmpl(value).appendTo("#productData");
            $("#loader").hide();
        }
    });
    return false;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions