{"id":476,"date":"2013-04-20T12:23:00","date_gmt":"2013-04-20T12:23:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=476"},"modified":"2025-02-21T09:50:32","modified_gmt":"2025-02-21T09:50:32","slug":"javascript-mv-frameworks","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/javascript-mv-frameworks","title":{"rendered":"Exploring JavaScript MV Frameworks Part 1 \u2013 Hello Backbonejs"},"content":{"rendered":"\n<p>JavaScript has become one of the most popular programming languages on the web. At first, developers didn\u2019t take it seriously, simply because it was not intended for server side programming.<\/p>\n\n\n\n<p>It was a common misconception among professional developers that this language was meant for \u201cAmateurs\u201d as it focused only on User Interface. JavaScript got the spotlight when the usage of Ajax came to light and professional programmers gave importance to the responsiveness of the page. But now the language has become more popular than ever as the User Experience has become the key part of web development. Accessing web is not limited to browsers alone \u2013 there are lot many devices with varying screen sizes accessing the same content. With the rise of HTML5 and CSS3 the web will become more adaptive and responsive than ever and JavaScript plays a major role in it. It has also gained popularity in the server side programming which is made possible by <a href=\"http:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">NodeJS<\/a> framework.<\/p>\n\n\n\n<p>Increase in usage of JavaScript in modern applications demand developers to write maintainable code, separate concerns and improve testability. JavaScript is a \u201cclass\u201d less language and it was not designed to support Object Oriented Programming, however you can achieve similar results by workarounds. So if you are a developer from an Object Oriented Programming world, then you will find it hard until you get used to it. Though there are some DOM manipulation libraries like jQuery which simplifies client side scripting of HTML, they actually do not solve the problem of effectively handling separation of concerns. You will end up writing lot many jQuery selectors and callbacks to keep the data in sync between the HTML, JavaScript and the data fetched from the server and we\u2019re still stuck with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Spaghetti_code\" target=\"_blank\" rel=\"noopener noreferrer\">Spaghetti code<\/a>.<\/p>\n\n\n\n<p>Fortunately there are a few libraries and frameworks that come to rescue. Let\u2019s explore few concepts and libraries that assist in structuring JavaScript applications!<\/p>\n\n\n\n<p><em>This post is the first part of a blog series on JavaScript Frameworks and Libraries out there and I will be exploring BackboneJS here. Stay tuned for others!<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"what-is-mv\">What Is MV*?<\/h2>\n\n\n\n<p>Though all the frameworks out there somewhat tries to be MVC but they do not necessarily follow the pattern strictly. The idea of all the patterns is to separate Model, View and Logic that hooks the two behind which is the controller. However BackboneJS embeds the controller logic in the view itself though it efficiently maintains the separation. On the other side we do have other libraries which implement <a href=\"http:\/\/en.wikipedia.org\/wiki\/Model%E2%80%93view%E2%80%93presenter\" target=\"_blank\" rel=\"noopener noreferrer\">Model-View-Presenter(MVP)<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Model_View_ViewModel\" target=\"_blank\" rel=\"noopener noreferrer\">Model-View-ViewModel(MVVM)<\/a> pattern. For this reason we will refer these frameworks as MV* implementation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"what-is-mvc\">What Is MVC?<\/h2>\n\n\n\n<p>Model \u2013 View \u2013 Controller is an architectural pattern that has been around for a long time and is widely used in server side programming. There are a few frameworks like ASP.net MVC, Ruby on Rails etc. which help web developers to easily program them.<\/p>\n\n\n\n<p><b>Model<\/b> \u2013 refers to application data and business rules. (a.k.a domain model, entities)<\/p>\n\n\n\n<p><b>View<\/b> \u2013 what user sees! (HTML page in the web browser)<\/p>\n\n\n\n<p><b>Controller<\/b> \u2013 mediator between the two. Manipulates the model based on the user interaction. It handles all the logic.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.infragistics.com\/community\/cfs-file.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/nanil.metablogapi\/2604.MVC_2D00_Process_5F00_71DD0CBD.png\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/nanil\/metablogapi\/6472.MVC-Process_thumb_752E7E98.png\" alt=\"MVC-Process\" title=\"MVC-Process\"\/><\/a><\/figure>\n\n\n\n<p>Image Source: <a href=\"http:\/\/en.wikipedia.org\/wiki\/File:MVC-Process.png\" target=\"_blank\" rel=\"noopener noreferrer\">Wikipedia<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"mvc-in-javascript\">MVC In JavaScript?<\/h2>\n\n\n\n<p>Building single-page applications using JavaScript are getting popular these days and good examples of them are GMail and Google Docs. When you set out to build these type of applications you will most likely invent many of the pieces that make up an MV* coding paradigm. So instead you can make use of some of the famous libraries such as <a href=\"http:\/\/backbonejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">BackboneJS<\/a>, <a href=\"http:\/\/knockoutjs.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">KnockoutJS<\/a>, <a href=\"http:\/\/angularjs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">AngularJS<\/a>, <a href=\"http:\/\/emberjs.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">EmberJS<\/a> &#8230;. Let\u2019s explore these frameworks in detail starting with BackboneJS.&nbsp;&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"framework-or-just-a-library\">Framework Or Just a Library?<\/h2>\n\n\n\n<p>Before you pick to work on a particular JavaScript Framework or a Library, it\u2019s important to understand the difference between the two. <strong>Libraries<\/strong> just fit into your existing architecture and add a specific functionality whereas a <strong>Framework<\/strong> gives you an architecture and you will need to follow the rules. To make it simpler for you \u2013 Backbone and Knockout are JavaScript libraries and Ember and AngularJS are frameworks. As we explore them you will see the clear difference.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"hello-backbone\">Hello Backbone<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.infragistics.com\/community\/cfs-file.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/nanil.metablogapi\/6746.backbone_5F00_72FB0013.png\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/nanil\/metablogapi\/7651.backbone_thumb_257E5D86.png\" alt=\"Backbone_logo_horizontal\" title=\"Backbone_logo_horizontal\"\/><\/a><\/figure>\n\n\n\n<p>Backbone is a lightweight JavaScript library created by <a href=\"http:\/\/en.wikipedia.org\/wiki\/Jeremy_Ashkenas\" rel=\"noopener\">Jeremy Ashkenas<\/a> who is also known for <a href=\"http:\/\/coffeescript.org\/\" rel=\"noopener\">CoffeeScript<\/a>. It is designed for supporting Single page web application and has a dependency on <a href=\"http:\/\/underscorejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">UnderscoreJS<\/a> library which provides utility functions for common JavaScript tasks.<\/p>\n\n\n\n<p>With Backbone, data is represented as Models, which can be created, validated, and saved to the server. Views display the model&#8217;s state and it re-renders itself when a change is triggered(via a &#8220;change&#8221; event) in the model due to an UI interaction. This way Backbone provides a structured approach of keeping the data in sync with the HTML UI.<\/p>\n\n\n\n<p>Some of the major websites that used Backbone include <a href=\"http:\/\/www.usatoday.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">USA Today<\/a>, <a href=\"http:\/\/www.linkedin.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">LinkedIn Mobile<\/a>, <a href=\"http:\/\/hulu.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Hulu<\/a>, <a href=\"http:\/\/wordpress.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">WordPress<\/a>, <a href=\"https:\/\/foursquare.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Foursquare<\/a>, <a href=\"http:\/\/www.bitbucket.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Bitbucket<\/a>, <a href=\"http:\/\/www.khanacademy.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Khan Academy<\/a> and <a href=\"http:\/\/backbonejs.org\/#examples\" target=\"_blank\" rel=\"noopener noreferrer\">more..<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Getting started with Backbone<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Script Dependency<\/h4>\n\n\n\n<p>Backbone has a dependency on <a href=\"http:\/\/underscorejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">UnderscoreJS<\/a> or <a href=\"http:\/\/lodash.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Lo-Dash<\/a> for utility functions and relies on either <a href=\"http:\/\/jquery.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">jQuery<\/a> or <a href=\"http:\/\/zeptojs.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Zepto<\/a> for DOM manipulations. So make sure you add them to your page.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script type=\"text\/javascript\" src=\"..\/common\/jquery-1.8.3.min.js\">&lt;\/script>\n&lt;script type=\"text\/javascript\" src=\"..\/common\/lodash.js\">&lt;\/script>\n&lt;script type=\"text\/javascript\" src=\"js\/backbone.js\">&lt;\/script><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Backbone.Model<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$(function(){\n      var Person = Backbone.Model.extend({});\n      var person = new Person({name: \"James\", age: 51});\n      var name = person.get(\"name\");\n      var age = person.get(\"age\");\n      console.log(name + \":\" + age);\n});<\/pre>\n\n\n\n<p>To create a Model class of your own, you extend Backbone.Model and pass a JavaScript object to the constructor and set the attributes.&nbsp; You can then easily retrieve the values from a <strong>get<\/strong> function. You can alternatively set the attributes using a set function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Backbone.View<\/h4>\n\n\n\n<p>Set a template first:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;script type=\"text\/template\" id=\"person-template\">\n    &lt;div class=\"view\">\n        &lt;p>Name: &lt;input type=\"text\" value=\"&lt;%- name%>\"\/>&lt;\/p>\n        &lt;p>Age: &lt;input type=\"text\" value=\"&lt;%- age%>\"\/>&lt;\/p>\n    &lt;\/div>\n&lt;\/script><\/pre>\n\n\n\n<p>Set a container for rendering:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div id=\"container\">Your content will load in a bit..&lt;\/div><\/pre>\n\n\n\n<p>Define a view in the script:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/define view\nvar AppView = Backbone.View.extend({\nel: '#container',\nmodel: person,\ntemplate: _.template($(\"#person-template\").html()),\ninitialize: function(){this.render();},\nrender: function(){this.$el.html(this.template(this.model.toJSON()));}\n});\n\/\/ initialize view\nnew AppView();<\/pre>\n\n\n\n<p>Views in Backbone are almost more convention than they are code so you will&nbsp; have to rely on some JavaScript templating library like Underscore templates or Mustache.js to do a cleaner separation of the UI. In the above example I have used Underscore\u2019s templating solution to achieve the separation.&nbsp;<\/p>\n\n\n\n<p>Your own custom Views can be created with the help of Backbone.View.extend. There are few basic properties that you must be aware of to set the View.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>el \u2013 <\/strong>DOM element that the view will be rendered on. In this case it is &lt;div&gt; element with the id \u201ccontainer\u201d<\/li>\n\n\n\n<li><strong>$el \u2013 <\/strong>a cached jQuery (or Zepto) object for the view\u2019s element<\/li>\n\n\n\n<li><strong>model<\/strong>&nbsp;<strong>\u2013<\/strong> set the model data that was created using Backbone.Model<\/li>\n\n\n\n<li><strong>template \u2013<\/strong>&nbsp; Backbone is agnostic with respect to your preferred method of HTML templating.&nbsp; In this case Underscore\u2019s template function is used to set the template that is defined in the \u201cperson-template\u201d<\/li>\n\n\n\n<li><strong>initialize \u2013<\/strong> this function will be called by Backbone at the time of creation of the view<\/li>\n\n\n\n<li><strong>render \u2013<\/strong> this function is used to render the element with the view template and the data. In this case we replace the value in the \u201ccontainer\u201d with the templated view that consists of data from the model<\/li>\n<\/ul>\n\n\n\n<p>As stated in the Backbone\u2019s documentation \u2013 <em>the View class can also be thought of as a kind of controller dispatching events that originate from the UI, with the HTML template serving as the true view<\/em>. This leads to an <a href=\"http:\/\/lostechies.com\/derickbailey\/2011\/12\/23\/backbone-js-is-not-an-mvc-framework\/\" rel=\"noopener\">argument<\/a> whether or not Backbone follows real MVC principles. However if you don\u2019t learn it as a MVC library and give importance to the way it separates concerns, it should be fine.<\/p>\n\n\n\n<p>Some time ago Backbone did have its own <code>Backbone.Controller<\/code>, however it was renamed to <span style=\"font-family: 'Courier New';\">Router<\/span> as the naming for this component didn&#8217;t make sense from the way it was being set to use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">File Size, Download &amp; Other useful links<\/h3>\n\n\n\n<p><strong>File Size:<\/strong> 6.3Kb \u2013 minified and 56kb with Full Source and comments.<\/p>\n\n\n\n<p><strong>Download:<\/strong>&nbsp;<a href=\"http:\/\/backbonejs.org\/\" rel=\"noopener\">Backbone website<\/a><\/p>\n\n\n\n<p><strong>Annotated Source:<\/strong> <a title=\"http:\/\/backbonejs.org\/docs\/backbone.html\" href=\"http:\/\/backbonejs.org\/docs\/backbone.html\" rel=\"noopener\">http:\/\/backbonejs.org\/docs\/backbone.html<\/a><\/p>\n\n\n\n<p><strong>CDN:<\/strong> <a href=\"http:\/\/cdnjs.com\/\" rel=\"noopener\">cdnjs<\/a>, <a href=\"http:\/\/www.jsdelivr.com\/\" rel=\"noopener\">jsdelivr<\/a><\/p>\n\n\n\n<p><strong>Who is using it?:<\/strong> <a href=\"http:\/\/backbonejs.org\/#examples\" rel=\"noopener\">Check out their examples section<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"stay-tuned\">Stay Tuned<\/h2>\n\n\n\n<p>So that was a quick introduction to BackboneJS and an explanation on JavaScript frameworks in general. In upcoming posts you will be introduced to Ember, Knockout and Angular as well. So stay tuned!<\/p>\n\n\n\n<p>If you have any questions write to me <a href=\"mailto:nish@infragistics.com\">nish@infragistics.com<\/a> or find me on <a href=\"http:\/\/twitter.com\/NishAnil\" target=\"_blank\" rel=\"noopener noreferrer\">twitter @nishanil<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"\/products\/ignite-ui\/download\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/nanil\/metablogapi\/5807.css-less-nnish_thumb_3EEF540F.jpg\" alt=\"IgniteUI_Banner_300x250_a2\"\/><\/a><\/figure>\n\n\n\n<p>For this post, I am assuming you are a programmer like me and design is a planet just outside your universe. If you are from the design planet, you too can read on; may be there is something for you too!<\/p>\n\n\n\n<p>Writing CSS is fun, exciting and easy until your website grows fat with pages and complicated layouts. If you have ever tried fixing a layout in such a page \u2013 you know what I mean. Did I talk about fixing a layout? Oh yeah thanks to all the browsers.<\/p>\n\n\n\n<p>Many at times while writing CSS I wished I could write it more programmatically than just styling them. For e.g. If CSS had allowed variable declarations, I could have simply held the values in variables, perform few operations and re-use them in properties. But that\u2019s too much to ask for from a stylesheet language which should just do styling!<\/p>\n\n\n\n<p>Fortunately there are a few preprocessors like <a href=\"http:\/\/sass-lang.com\/\" rel=\"noopener\">Sass<\/a> &amp; <a href=\"http:\/\/lesscss.org\/\" rel=\"noopener\">LESS<\/a> that nicely extends CSS and adds everything a programmer ever wanted. After doing couple of research I picked up LESS to see how it works. I spent a few hours re-styling some of my demos with LESS and I am must say I am thoroughly impressed with this language. So here I\u2019m explaining you about LESS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"so-less\">So LESS<\/h2>\n\n\n\n<p>LESS is a dynamic stylesheet language that extends CSS and adds nice features like variables, mixins, operations and functions to it. More importantly it takes fewer efforts for developers to write complex CSS and build amazing looking websites real fast.&nbsp; LESS uses existing CSS syntax that makes learning a breeze and you can always fall back to CSS.<\/p>\n\n\n\n<p>LESS\u2019 first version was written in Ruby and was used as a server side language which upon compiling emitted CSS. However in the later versions, use of Ruby is deprecated and replaced by JavaScript. Adding LESS.js JavaScript file to your HTML page allows real-time compilation within browsers. It also supports server side compiling with the help of <a href=\"http:\/\/nodejs.org\/\" rel=\"noopener\">Node.js<\/a> making it easier for developers to choose between the two.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Less.js \u2013 Client-Side Usage<\/h3>\n\n\n\n<p>All LESS files should have the extension \u201c.less\u201d and you may have them under CSS directory of your webserver.<\/p>\n\n\n\n<p>Add the following lines of code to your HTML page to ensure Pre-Compiling of CSS happens in your browser:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;link rel=\"stylesheet\/less\" type=\"text\/css\" href=\"css\/style.less\"\/>\n&lt;script src=\"js\/less-1.3.3.min.js\">&lt;\/script><\/pre>\n\n\n\n<p><em>Note: If you working on a local file system i.e. if you are accessing the page using \u201cfile:\/\/\/\u201d on Chrome or IE you may incur an error of \u201cCross origin requests are only supported for HTTP\u201d or \u201cAccess Denied\u201d respectively. These are some security related errors and I did not find a way to get rid of them. Host them on a development server and you will see this go away. However I did not find any issues with Mozilla Firefox.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Server Side Usage<\/h3>\n\n\n\n<p>If performance is what running in your mind then you can look at compiling these files on the server side. First, download and install Node.js, then using <a href=\"https:\/\/npmjs.org\/\" rel=\"noopener\">npm<\/a>&nbsp; download the less compiler(<em>lessc.cmd<\/em>).<\/p>\n\n\n\n<p>To compile use this command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">lessc styles.less > styles.css<\/pre>\n\n\n\n<p>For more options like CSS minifying, run <em>lessc<\/em> without parameters.<\/p>\n\n\n\n<p>There are few nice editors out there that will let you live compile these files. For e.g. I use WebStorm which nicely compiles LESS to CSS as I type. If you are on a large project and most of your developers are comfortable on LESS then you can add the sever side compilation step to your build task.<\/p>\n\n\n\n<p>Now that you have know how to make \u201c<em>.less<\/em>\u201d files and compile them to CSS, let\u2019s look at this language in detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"variables\">Variables<\/h2>\n\n\n\n<p>As mentioned earlier variables are one nice feature to have in stylesheets. LESS let\u2019s you add variables with the help of <em>@ symbol<\/em> and use them into properties. Find below an example where I set background-color and color of the body with the help of variables.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@backgroundColor: #333;\n@color: #fff;\n\nbody {\n  background-color: @backgroundColor;\n  color: @color;\n  border-top: solid 10px #000;\n  font-size: .85em;\n  font-family: \"Segoe UI\", Verdana, Helvetica, Sans-Serif;\n  margin: 0;\n  padding: 0;\n}<\/pre>\n\n\n\n<p>These variables can be now re-used in the rest of the code and any change that you make to color will apply to all. CSS codes can co-exist with LESS \u2013 If you notice only two of the properties were set by variables and the rest is CSS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"operations\">Operations<\/h2>\n\n\n\n<p>Now that you know variables are a possibility in stylesheet, you must be happy to know that you can perform operations on them. It\u2019s easy! Here\u2019s is an example of how to do it:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@baseColor: #000;\n@backgroundColor: (@baseColor + #333);\n@color: (@backgroundColor \/ 3);\n@font-family: \"Segoe UI\", Verdana, Helvetica, Sans-Serif;\n@font-Size: 1em;\n#body {\n  background-color: @backgroundColor;\n  color: @color;\n  border-top: solid 10px @baseColor;\n  font-size: (@font-Size - .15em);\n  font-family: @font-family;\n}<\/pre>\n\n\n\n<p>Have a look at how <em>@backgroundColor<\/em>, <em>@color<\/em> and <em>font-size<\/em> gets the calculated value from an operation. Find below the output that gets generated.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#body {\n  background-color: #333333;\n  color: #111111;\n  border-top: solid 10px #000000;\n  font-size: 0.85em;\n  font-family: \"Segoe UI\", Verdana, Helvetica, Sans-Serif;\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"mixins\">Mixins<\/h2>\n\n\n\n<p>Mixins help you reuse the whole bunch of properties from one ruleset into another ruleset. Here\u2019s an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@baseColor: #000;\n@font-family: \"Segoe UI\", Verdana, Helvetica, Sans-Serif;\n@font-Size: 1em;\n\n.gradients {\n  \/*local scoped variables*\/\n  @gradientStartColor: #eaeaea;\n  @gradientEndColor: #cccccc;\n  background: @baseColor; \/* variable from global scope *\/\n  background: linear-gradient(top, @gradientStartColor, @gradientEndColor);\n  background: -o-linear-gradient(top, @gradientStartColor, @gradientEndColor);\n  background: -ms-linear-gradient(top, @gradientStartColor, @gradientEndColor);\n  background: -moz-linear-gradient(top, @gradientStartColor, @gradientEndColor);\n  background: -webkit-linear-gradient(top, @gradientStartColor, @gradientEndColor);\n}\n\n#body {\n .gradients;\n  border-top: solid 10px @baseColor;\n  font-size: (@font-Size - .15em);\n  font-family: @font-family;\n}<\/pre>\n\n\n\n<p>In the above code you can see how <em>.gradients<\/em> ruleset is re-used into <em>#body<\/em>. It\u2019s a pretty nice feature, think about how less code you need to write now!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Variable Scoping<\/h3>\n\n\n\n<p>Like every other programming language, LESS provides variable scoping too. Variables are looked up locally first and when not found it will search globally. In the above example you can see that the<em> @baseColor<\/em> is being used in both<em> .gradients<\/em> and <em>#body<\/em>. If you have locally scoped variables like <em>@gradientStartColor<\/em> and <em>@gradientEndColor<\/em> they are not accessible outside the scope unless they are mixed in. In the above example <em>#body<\/em> can access those variables inside since <em>.gradients<\/em> is referred.<\/p>\n\n\n\n<p>Check out <a href=\"http:\/\/lesscss.org\/#-scope\" rel=\"noopener\">Scope<\/a> for more info.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"parametric-mixins\">Parametric Mixins<\/h2>\n\n\n\n<p>This is special type of ruleset which can be mixed in like classes, but accepts parameters. Here\u2019s an example that sets up border-radius for different browsers.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.border-radius (@radius: 4px) {\n  border-radius: @radius;\n  -moz-border-radius: @radius;\n  -webkit-border-radius: @radius;\n}\n#body {\n .gradients;\n  .border-radius;\n  border-top: solid 10px @baseColor;\n}\n#sidebar{\n  .border-radius(25px);\n  background: #eee;\n}<\/pre>\n\n\n\n<p>If you look at <em>#body<\/em> it calls without parameters that\u2019s because LESS allows to set default values of parameters which in this case is <em>4px<\/em>. Look at <em>#sidebar<\/em> for call with parameters. You can also set multiple parameters, <a href=\"http:\/\/lesscss.org\/#-parametric-mixins\" rel=\"noopener\">check this out<\/a> for more information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"functions\">Functions<\/h2>\n\n\n\n<p>LESS provides few helper functions for transforming colors, string manipulation, and do math. Find below an example from the LESS documentation which uses percentage to convert 0.5 to 50%, increases the saturation of a base color by 5% and then sets the background color to one that is lightened by 25% and spun by 8 degrees:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#sidebar{\n  width: percentage(0.5);\n  color: saturate(@baseColor, 5);\n  background-color: spin(lighten(#ff0000, 25%), 8);\n}<\/pre>\n\n\n\n<p>Check out <a href=\"http:\/\/lesscss.org\/#reference\" rel=\"noopener\">Function Reference<\/a> for details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"summary\">Summary<\/h2>\n\n\n\n<p>By now you would have a fair idea of what LESS brings to the table. But be aware that LESS is not the only CSS preprocessor. There is Sass which stands for <a href=\"http:\/\/sass-lang.com\/\" rel=\"noopener\">Syntactically Awesome Stylesheets<\/a> and few others but they aren\u2019t popular. There are various blogs out there which will tell you some good comparison between the two. I suggest you try both and stick to the syntax that you like! After all they emit CSS <img decoding=\"async\" class=\"wlEmoticon wlEmoticon-smile\" style=\"border-style: none;\" src=\"http:\/\/www.infragistics.com\/community\/cfs-file.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/nanil.metablogapi\/2112.wlEmoticon_2D00_smile_5F00_53EFB27E.png\" alt=\"Smile\"><\/p>\n\n\n\n<p>Have a feedback? Find me on <a href=\"http:\/\/twitter.com\/NishAnil\" rel=\"noopener\">twitter @nishanil<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"\/products\/ignite-ui\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/nanil\/metablogapi\/2703.0640.IgniteUI_Banner_728x90_b_32FC3FD7.jpg\" alt=\"0640.IgniteUI_Banner_728x90_b\"\/><\/a><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript has become one of the most popular programming languages on the web. At first, developers didn\u2019t take it seriously, simply because it was not intended for server side programming.<\/p>\n","protected":false},"author":21,"featured_media":2210,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=476"}],"version-history":[{"count":2,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":2027,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/476\/revisions\/2027"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2210"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}