Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Backbone.LayoutManager

Provides a logical foundation for assembling layouts and views within Backbone. Designed to be adaptive and configurable for painless integration. Well tested, with full code coverage, in both the browser and Node.js environments.

Depends on Underscore, Backbone, and jQuery. You can swap out the dependencies with a custom configuration.

Install

[sudo] npm install grunt-cli –g

Example usage

Main Layout
<script id="main-layout" type="layout">
  <section class="content"></section>

  <!-- Login template below will be injected here -->
  <aside class="secondary"></aside>
</script>
Login Template
<script id="login-template" type="template">
  <form class="login">
    <p><label for="user">Username</label>
       <input type="text" name="user"></p>
    <p><label for="pass">Password</label>
       <input type="text" name="pass"></p>
    <p><input class="loginBtn" type="submit" value="Login">
    </p>
  </form>
</script>

Javascript MVC/MVVM Frameworks, AngularJS vs Backbone vs Knockout

AngularJS

Pros

-Uses primitive javascript types instead of function wrappers (“dirty checking”)
-Loaded with functionality
-Easy to get started
-Fast development and smaller amounts of boiler-plate code compared to backbone
-Makes testing easy and offers many testing tools
-Working with standards bodies to make browsers operate in the same way as angular

Cons

-Takes longer to learn than knockout and backbone
-Has not been proven in as many mainstream projects compared to backbone.js

Backbone

Pros

-Relatively mature, proven framework with a strong community behind it
-Many extensions and scaffolding tools available
-Flexible, works fine for a new project or improving an existing one

Cons

-Lacks data-binding
-Requires a large amount of boiler-plate code
-Could be dangerous if programmers do not follow proper conventions

Knockout

Pros

-Highly compatible with other 3rd party js libraries
-Easy to learn and use
-Dependencies are handled through a dependency graph which targets specific data as opposed to updating entire models when data changes like in Angular.js. This may increase performance in data-heavy applications compared to “dirt checking” which Angular uses.

Cons

-All javascript variables and arrays are functions (aka KO observables) as opposed to angular which can be a little confusing at first for some people. But it should be noted that all native javascript array functions like splice(), indexOf(), push() etc. are implemented by Knockout and may be used on KO observables.
-HTML views can get messy as declarative bindings increase. This can be mitigated to some extent through the use of custom bindings, computed observables and by attaching events to the DOM using JQuery instead of using the data-bind attribute.
-Additional functionality like data-access and url-routing are not included. If you are looking for an end to end solution that offers a complete toolbox of common web-app functionality, you should probably check out frameworks like Angular or Ember.

AngularJS – a perfect javascript MVC framework

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML’s syntax to express your application’s components clearly and succinctly. Out of the box, it eliminates much of the code you currently write through data binding and dependency injection. And it all happens in JavaScript within the browser making it an ideal partner with any server technology.

Angular is what HTML would have been had it been designed for applications. HTML is a great declarative language for static documents. It does not contain much in the way of creating applications, and as a result building web applications is an exercise in what do I have to do, so that I trick the browser in to doing what I want.

The impedance mismatch between dynamic applications and static documents is often solved as:

  • library – a collection of functions which are useful when writing web apps. Your code is in charge and it calls into the library when it sees fit. E.g., jQuery.
  • frameworks – a particular implementation of a web application, where your code fills in the details. The framework is in charge and it calls into your code when it needs something app specific. E.g., knockout, sproutcore, etc.

Angular takes another approach. It attempts to minimize the impedance mismatch between document centric HTML and what an application needs by creating new HTML constructs. Angular teaches the browser new syntax through a construct we call directives. Examples include:

  • Data binding as in {{}}.
  • DOM control structures for repeating/hiding DOM fragments.
  • Support for forms and form validation.
  • Attaching code-behind to DOM elements.
  • Grouping of HTML into reusable components.

Web site: http://angularjs.org

Source: https://github.com/angular/angular.js

Tutorial: http://docs.angularjs.org/tutorial

Overview: http://docs.angularjs.org/guide/overview

API Docs: http://docs.angularjs.org/api

Developer Guide: http://docs.angularjs.org/guide

 

Key Points
  • Maintained by google

Maintained by google and well documented. And there are also lots of demos on the internet.

  • DOM Manipulation

Other frameworks deal with HTML’s shortcomings by either abstracting away HTML, CSS, and/or JavaScript or by providing an imperative way for manipulating the DOM. Neither of these address the root problem that HTML was not designed for dynamic views.

  • Extensibility

AngularJS is a toolset for building the framework most suited to your application development. It is fully extensible and works well with other libraries. Every feature can be modified or replaced to suit your unique development workflow and feature needs. Read on to find out how.

  • Deep Linking

A deep link reflects where the user is in the app, this is useful so users can bookmark and email links to locations within apps. Round trip apps get this automatically, but AJAX apps by their nature do not. AngularJS combines the benefits of deep link with desktop app-like behavior.

  • Form Validation

Client-side form validation is an important part of great user experience. AngularJS lets you declare the validation rules of the form without having to write JavaScript code. Write less code, go have beer sooner.

  • Server Communication

AngularJS provides services on top of XHR, that dramatically simplify your code. We wrap XHR to give you exception management and promises. Promises further simplify your code by handling asynchronous return of data. This lets you assign properties synchronously when the return is actually asynchronous.

  • Directives

Directives is a unique and powerful feature available only in Angular. Directives let you invent new HTML syntax, specific to your application.

  • Reusable Components

We use directives to create reusable components. A component allows you to hide complex DOM structure, CSS, and behavior. This lets you focus either on what the application does or how the application looks separately.

  • Localization

An important part of serious apps is localization. Angular’s locale aware filters and stemming directives give you building blocks to make your application available in all locales.

  • Embeddable

AngularJS works great with other technologies. Add as much or as little of AngularJS to an existing page as you like. Many other frameworks require full commitment. This page has multiple AngularJS applications embedded in it. Because AngularJS has no global state multiple apps can run on a single page without the use of iframes. We encourage you to view-source and look around.

  • Injectable

The dependency injection in AngularJS allows you to declaratively describe how your application is wired. This means that your application needs no main() method which is usually an unmaintainable mess. Dependency injection is also a core to AngularJS. This means that any component which does not fit your needs can easily be replaced.

  • Testable

AngularJS was designed from ground up to be testable. It encourages behavior-view separation, comes pre-bundled with mocks, and takes full advantage of dependency injection. It also comes with end-to-end scenario runner which eliminates test flakiness by understanding the inner workings of AngularJS.

Data Binding

There are many templating systems out there. Most of them consume a static string template and combine it with data, resulting in a new string. The resulting text is then innerHTMLed into an element.

One Way Data Binding.png

This means that any changes to the data need to be re-merged with the template and then innerHTMLed into the DOM. Some of the issues with this approach are: reading user input and merging it with data, clobbering user input by overwriting it, managing the whole update process, and lack of behavior expressiveness.

Angular is different. The Angular compiler consumes the DOM with directives, not string templates. The result is a linking function, which when combined with a scope model results in a live view. The view and scope model bindings are transparent. No action from the developer is needed to update the view. And because no innerHTML is used there are no issues of clobbering user input. Furthermore, Angular directives can contain not just text bindings, but behavioral constructs as well.

Two Way Data Binding.png

The Angular approach produces a stable DOM. This means that the DOM element instance bound to a model item instance does not change for the lifetime of the binding. This means that the code can get hold of the elements and register event handlers and know that the reference will not be destroyed by template data merge.

MVC

While Model-View-Controller (MVC) has acquired different shades of meaning over the years since it first appeared, Angular incorporates the basic principles behind the original MVC software design pattern into its way of building client-side web applications.

The MVC pattern summarized:

  • Separate applications into distinct presentation, data, and logic components
  • Encourage loose coupling between these components

Along with services and dependency injection, MVC makes angular applications better structured, easier to maintain and more testable.

Controller

Concepts-controller.png

A controller is the code behind the view. Its job is to construct the model and publish it to the view along with callback methods. The view is a projection of the scope onto the template (the HTML). The scope is the glue which marshals the model to the view and forwards the events to the controller.

The separation of the controller and the view is important because:

  • The controller is written in JavaScript. JavaScript is imperative. Imperative is a good fit for specifying application behavior. The controller should not contain any rendering information (DOM references or HTML fragments).
  • The view template is written in HTML. HTML is declarative. Declarative is a good fit for specifying UI. The View should not contain any behavior.
  • Since the controller is unaware of the view, there could be many views for the same controller. This is important for re-skinning, device specific views (i.e. mobile vs desktop), and testability.
Model

Concepts-model.png

The model is the data which is merged with the template to produce the view. To be able to render the model into the view, the model has to be able to be referenced from the scope. Unlike many other frameworks Angular makes no restrictions or requirements on the model. There are no classes to inherit from or special accessor methods for accessing or changing the model. The model can be primitive, object hash, or a full object Type. In short the model is a plain JavaScript object.

View

Concepts-view.png

The view is what the user sees. The view begins its life as a template, is merged with the model and finally rendered into the browser DOM. Angular takes a very different approach to rendering the view compared to most other templating systems.

  • Others – Most templating systems begin as an HTML string with special templating markup. Often the template markup breaks the HTML syntax which means that the template can not be edited by an HTML editor. The template string is then parsed by the template engine, and merged with the data. The result of the merge is an HTML string. The HTML string is then written to the browser using the .innerHTML, which causes the browser to render the HTML. When the model changes the whole process needs to be repeated. The granularity of the template is the granularity of the DOM updates. The key here is that the templating system manipulates strings.
  • Angular – Angular is different, since its templating system works on DOM objects not on strings. The template is still written in an HTML string, but it is HTML (not HTML with template sprinkled in.) The browser parses the HTML into the DOM, and the DOM becomes the input to the template engine known as the compiler. The compiler looks for directives which in turn set up watches on the model. The result is a continuously updating view which does not need template model re-merging. Your model becomes the single source-of-truth for your view.

Bootstrap Paginator

Bootstrap Paginator is a jQuery plugin that simplifies the rendering of Bootstrap Pagination component. It provides methods to automates the update of the pagination status and also some events to notify the status changes within the component.Bootstrap Paginator is able to run on the following browsers: Firefox 5+, Chrome 14+, Safari 5+, Opera 11.6+ and IE 7+.

Bootstrap Paginator will have the Bootstrap Pagination Component rendered as shown in the following graph. It has 5 parts. Left most one is the go to first page item. The one next to it is the previous page item. The following numeric page items are of the type page. Next page item comes after it. Last page item will be the last one rendered. Please note that the labels are the types of each component.

With this plugin, you can easily write your customered pager control in bootstrap style. Here is the github location: https://github.com/lyonlai/bootstrap-paginator

Sticky Footer , fixed footer, pinned footer

1. through css

    #footer {

   position:fixed;

   left:0px;

   bottom:0px;

   height:30px;

   width:100%;

   background:#999;

}

2. jQuery Easy Pinned Footer

http://johnpatrickgiven.com/jquery/pinned-footer/

Sticky Footer , fixed footer, pinned footer

1. through css

    #footer {

   position:fixed;

   left:0px;

   bottom:0px;

   height:30px;

   width:100%;

   background:#999;

}

2. jQuery Easy Pinned Footer

http://johnpatrickgiven.com/jquery/pinned-footer/