<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dima Kuzmich on Medium]]></title>
        <description><![CDATA[Stories by Dima Kuzmich on Medium]]></description>
        <link>https://medium.com/@forforeach?source=rss-46c67831b471------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2bC1x9S3t84rz7EepjUmjA.png</url>
            <title>Stories by Dima Kuzmich on Medium</title>
            <link>https://medium.com/@forforeach?source=rss-46c67831b471------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 14:42:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@forforeach/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Test Angular2 now or how to set up Angular2 unit tests]]></title>
            <link>https://medium.com/@forforeach/test-angular2-now-or-how-to-set-up-angular2-unit-tests-c46f5dcbba78?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/c46f5dcbba78</guid>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[web-developer]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Fri, 21 Aug 2015 12:57:43 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:36.222Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="angular" src="https://cdn-images-1.medium.com/max/184/0*IMq42cwdJAeOSBDb.png" /></figure><p>Currently I’m working on a project and I decided to build it with Angular2. It was a brave decision. It’s really not as simple as it looks, when you try to build something with an alpha versions of a new technology. Loads of changes that part of them are breaking, difficulty in lack of documentation. Nevertheless, it’s incredibly exciting to see how Angular is growing up, to observe a collaboration of the team and to feel a part of this interesting process.</p><p>One of the tasks that wasn’t trivial for me is to set up the unit tests for my project. That’s the reason for my decision to describe this process. So let’s begin:</p><h4><strong>TypeScript</strong></h4><p>As <a href="http://blogs.msdn.com/b/typescript/archive/2015/03/05/angular-2-0-built-on-typescript.aspx">Microsoft published</a> nearly half-a-year ago, Angular2 is written in TypeScript. At the beginning that was a shocking news for me. But later on I’ve understood this step. I checked out a new version of TypeScript and was surprised to discover that it’s pretty close to ES6, erhh… JavaScript 2015?!.. Mmm… never mind, the latest JavaScript version :-) It also brings additional features, such as optional types, annotations and more. These facts were convincing enough to try to write my project with TypeScript. I think that is’t pretty clear, that if a code is written in TypeScript, tests should be written in TypeScript as well. If the tests are written in TypeScript, i should transpile them into ES5. And so I did with two tasks of <a href="http://gulpjs.com/">Gulp</a>:</p><figure><img alt="gulp typescript tests build" src="https://cdn-images-1.medium.com/max/881/0*CzvQjx7-jSOhvE_F.png" /></figure><p>Source code is <a href="https://github.com/forforeach/Angular2-Unit-Tests/blob/master/gulpfile.js">here</a>.</p><h4><strong>Karma</strong></h4><p><a href="http://karma-runner.github.io/0.13/index.html">Karma </a>is a test runner that was developed by Angular team during development of <a href="https://angularjs.org/">Angular.js</a>. Among its main features is the ability to run tests fast and on real browsers. Also it’s agnostic to testing framework so that you can use whatever framework you’d like. I chose a <a href="http://jasmine.github.io/">Jasmine</a>, but you can easily switch it to another one. So I installed Karma and Jasmine on my environment and started to set up my first test. here is an interesting part of the file:</p><figure><img alt="karma config" src="https://cdn-images-1.medium.com/max/732/0*xwsNxmNMFfMidCu6.png" /></figure><p>Source code is <a href="https://github.com/forforeach/Angular2-Unit-Tests/blob/master/karma.conf.js">here</a>.</p><p>First of all we need to add traceur-runtime.js. These are basically polyfills that allow us to use ES6 features in the browser despite their not being implemented yet.</p><p>Then we add our source files. The files should already be transpiled into ES5 code and should be only served by Karma and not actually be included with a script tag. That’s because we will import them into the tests using <a href="https://github.com/systemjs/systemjs">System.js</a>.</p><p>And here they are es6 module loader and system.js files. System.js is universal dynamic module loader that loads ES6 modules, AMD, CommonJS and global scripts in the browser and NodeJS. Later on we will see how to configure System.js to work with our tests properly.</p><p>We should definitely add angular2 file :-) and then our test files. They also should be served and not included.</p><p>Surely, you noticed this <strong>test-main.js</strong> file that I included but didn’t explain. This file is the highlight of this post! So, Ladies and Gentelmen please welcome:</p><h4>The System.js hook</h4><p>As I said before, in order to support modules’ imports we use System.js. Currently Karma does not support this feature, so that we need to implement some workaround that will be able to import all needed modules and only then to run our tests. Here is what we need to do:</p><figure><img alt="karma systemjs hook" src="https://cdn-images-1.medium.com/max/795/0*o60UgRlAOIQGLPyY.png" /></figure><p>First of all, you should stop karma from executing. This is made by</p><pre>__karma__.loaded = function () { };</pre><p>Next, we should configure our system.js with appropriate files by setting baseUrl and paths properties of System global object.</p><pre>System.baseURL = ‘/base/scripts/’;<br>System.paths = {<br> ‘test/*’: ‘/base/scripts/test/*.js’,<br> ‘build/*’: ‘/base/scripts/build/*.js’,<br> ‘angular2/*’: ‘angular2/*’,<br> ‘rx’: ‘rx’<br>};</pre><p>Then we take all files that served by Karma, filter only test files, normalize file names to module names and import them one-by-one using System.import method.</p><p><strong>One important thing to notice</strong>: each test file has to export main function that includes whole tests related to a module. When module is imported, we run its main method in order to expose the tests. Here is an example of a test module:</p><figure><img alt="test example" src="https://cdn-images-1.medium.com/max/596/0*JOflARPq9HsC-Kmj.png" /></figure><p>Lastly, when entire tests were imported and exposed we run the karma engine:</p><pre>__karma__.start(); // after all tests were exposed</pre><p>And voilà, our Angular2 test environment is ready!</p><p>You can find the <a href="https://github.com/forforeach/Angular2-Unit-Tests">source code on Github</a>. To set up the environment, just clone the repository and run:</p><pre>$ npm start</pre><p>Also you are welcome to check out an <a href="https://github.com/EladRK/angular-starter">angular-starter</a> that my college Elad and I developed. It’s really simple and uses gulp for a build workflow.</p><p>I’ve based my tests environment on an angular tests environment. You can find their tests-main.js <a href="https://github.com/angular/angular/blob/master/test-main.js">here</a>.</p><p>One more thing, if you liked this post, please share it. For any question you can find <a href="https://twitter.com/kuzmich_dima">@me on Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c46f5dcbba78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prevent Gulp from stopping watching of files after exception]]></title>
            <link>https://medium.com/@forforeach/prevent-gulp-from-stopping-watching-of-files-after-exception-d2a476601aef?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2a476601aef</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[gulp]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[web-developer]]></category>
            <category><![CDATA[jsx]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Thu, 26 Feb 2015 10:43:39 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:10.788Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I did some small project and decided to try and build automated workflow with <a href="http://gulpjs.com/">Gulp</a>. It was very fun and easy. I really liked its features and node-like syntax.</p><p>My project included <a href="http://facebook.github.io/react/">React.js</a> and I used their <a href="http://facebook.github.io/react/docs/jsx-in-depth.html">JSX</a> technology. I wanted to debug my code, thus I needed to compile JSX into vanilla JavaScript. Here is where Gulp came to my rescue. You can easily install gulp-react and it does the magic.</p><p>After building my tasks and integrating a compiling I wanted to run my tasks constantly. That’s not a problem when you use Gulp. Just use <strong>gulp.watch()</strong> and there you are. But after I’ve started to change code, I’ve understood, that my compiled file isn’t updating. When I took a look on a console, I noticed the exception and after it my watching task stopped:</p><figure><img alt="exception" src="https://cdn-images-1.medium.com/max/678/0*CEJri9UZA7ZRgnPg.png" /></figure><p>The fix for this problem was another gulp package that called <a href="https://www.npmjs.com/package/gulp-plumber">gulp-plumber</a>. This plugin prevents pipe breaking caused by exceptions. Generally saying, it overrides pipe method of gulp and removes <strong>onerror </strong>handler which unpipes streams on error by default.</p><p>To sum it up, it was really good experience using Gulp and its plugins.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2a476601aef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Binding event to all elements except specific element and all its contents.]]></title>
            <link>https://medium.com/@forforeach/binding-event-to-all-elements-except-specific-element-and-all-its-contents-41a2d4da9d59?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/41a2d4da9d59</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[javascript-event-handler]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Wed, 28 May 2014 09:29:23 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:11.203Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I’ve encountered a challenge to close navigation menu on every touch or click, except clicks and touches in the menu area. I’ve tried many ways to resolve it, and at last I’ve found the optimal solution for me. The solution is in two steps and rely on event bubbling.</p><p>Firstly, I’ve attached click/touch event on the &lt;body&gt; that on every click/touch closes the menu.</p><pre>function clickOnBody(event){<br> // you can close here the menu<br> alert(‘clicked everywhere except the menu area’);<br>}<br>document.body.addEventListener(‘mousedown’, clickOnBody, false);<br>document.body.addEventListener(‘touchstart’, clickOnBody, false);</pre><p>Secondly, I’ve attached another click/touch event on specific element, and inside this event I’ll prevent event propagation (bubbling), so that every click/touch inside element area will stop on the parrent element.</p><pre>function clickOnMenu(event){<br> event.stopPropagation();<br>}<br>var menu = document.getElementById(‘menu’);<br>menu.addEventListener(‘mousedown’, clickOnMenu, false);<br>menu.addEventListener(‘touchstart’, clickOnMenu, false);</pre><p>You can check the solution in <a href="http://jsfiddle.net/dima_k/d9JRu/1/">jsFiddle</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41a2d4da9d59" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Memory leaks in SPA built with Knockout.js ]]></title>
            <link>https://medium.com/@forforeach/memory-leaks-in-spa-built-with-knockout-js-4900b8ec73bc?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/4900b8ec73bc</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[sap]]></category>
            <category><![CDATA[knockoutjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Mon, 28 Apr 2014 12:08:03 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:29.551Z</atom:updated>
            <content:encoded><![CDATA[<h3>Memory leaks in SPA built with Knockout.js</h3><p><strong>The Problem:</strong></p><p>When we build large SPA applications with MV* frameworks that have data bindings, such as Knockout.js, we actually create connection between our JS models and DOM elements. Those connections may be done in number of ways, but the bottom line is that DOM elements may have references to our models. In Knockout, for example, every element that has bindings, has relation to closure object that stores reference to binding context. This binding context has pointer to our model. Now, with this in mind, we can see the problem that will come. If we simply remove DOM element that has bindings, a closure from above will still hold a pointer to our model and as a result will be never released from memory. This problem is especially critical in SPA applications as to change page we simply swap inner HTML of some container and never do full reload of the page.</p><p><strong>Detection:</strong></p><p>To detect an above problem in SPA with Knockout we don’t need to do much. If we bind our models and never unbind them, we almost certain have a leaks. However, I’ll show a way to ensure it with Chrome DevTools.</p><p>We need to run our application and to open a DevTools (F12 for windows users). I created a simple application with Knockout and Sammy.js for routing. <a href="https://docs.google.com/uc?id=0B3tmSeYWpEi4eWxSRERuenVINGc&amp;export=download">Download it here</a>. After we run an application we need to take a heap snapshot. Press ‘Profiles’ tab in DevTools, check ‘Take Heap Snapshot’ and press ‘Take Snapshot’</p><figure><img alt="Image" src="https://cdn-images-1.medium.com/max/650/0*JEnfYClDIWNYHHdg." /></figure><p>After that change between pages 1 and 2 for 10 times and take another snapshot. Now, when we have these two snapshots, we can compare them. We simply switch the mode of snapshot preview to ‘Comparison’</p><figure><img alt="Image" src="https://cdn-images-1.medium.com/max/650/0*ItFlmXxmIziU8MQg." /></figure><p>While in comparison mode, we can sort our results by ‘# Delta’ field. This field says how many objects from particular type are living now in memory. You can also check how many new objects was created and how many objects was released by looking at ‘# New’ and ‘# Deleted’ fields respectively. What we are looking now are the objects that created and never released. In my test application is very simple. We know that we visited each page for 5 times, so I will look for an objects that have 5 in ‘Delta’ field.</p><figure><img alt="Image" src="https://cdn-images-1.medium.com/max/650/0*MAlt1jCKsOHD6pUN." /></figure><p>As we can see, neither VM1 nor VM2 was released during our session.</p><p><strong>Solution:</strong></p><p>The solution for this problem, is very simple. As I said before, we only need to release bindingContext object of DOM node that we want to remove. Knockout provides us with number of methods that could be useful in this situation. We can use a ko.cleanNode(element) method before we remove an HTML or we can use ko.removeNode(element), that cleans Node and also removes it. No matter which way we are choosing to use, we need always remember <strong>— If we created a bindings, we must to unbind them!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4900b8ec73bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Global Object, Namespaces and Object constructors]]></title>
            <link>https://medium.com/@forforeach/javascript-global-object-namespaces-and-object-constructors-d48f7e20bdb9?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/d48f7e20bdb9</guid>
            <category><![CDATA[namespaces]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[constructor]]></category>
            <category><![CDATA[javascript-architecture]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Sat, 09 Nov 2013 14:47:15 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:27.013Z</atom:updated>
            <content:encoded><![CDATA[<h4>Global Object</h4><p>Global Object is an object that created once the application is running. We can’t recreate it or affect his creation in any way. After a creation Global Object uses as a global scope of the application. Actually, in JavaScript we haven’t global variables and functions. All variables and functions that defined globally become properties of the Global Object. So, all variables in JavaScript are placed under Global Object. ECMA-262 does not indicate a way to access the Global object directly, but we can implement the access to him by using this.</p><p>The example below shows you how to do it:</p><pre>var globalObject = (function(){<br> return this;<br>})();</pre><p><a href="http://jsfiddle.net/dima_k/2Apbb/2/">Check it on jsFiddle</a></p><p>In web applications the browser implementation of ECMA-262 has an object called window that uses as a delegate to a Global Object, so all the way we can access him very simply:</p><pre>var globalObject = window;</pre><p>In above examples we created variables that hold a reference to Global Object. As discussed previously, all global variables become a properties of Global Object, so we simply created properties on Global Object that reference to himself.</p><p>Because all new variables and functions are placed under Global Object, there is a very high chance of names collision. As we know, JavaScript is loosely typed language and as such he is not preserving a type for created variables. So, all variables can overhead other variables just by using same name. This is a names collision that we talked before.</p><p>To prevent a name collision we can use a number of techniques such as using namespaces, using modules and more.</p><h4>Namespaces</h4><p>Namespaces are objects that created under Global Object and hold variables and functions. Think about it like a tree. Tree has a root (Global object) and branches. Every branch is a new namespace.</p><figure><img alt="Variables-tree" src="https://cdn-images-1.medium.com/max/457/0*odnDAywZzUMJzYoi.png" /></figure><p>To implement it in JavaScript we use next code:</p><pre>var myNamespace = myNamespace || {};</pre><p>Left side of the example above simply declares a variable for our namespace. Right side a little bit more complicated. First of all we try to get a namespace with same name, if declared somewhere in application. This technique prevents recreation of same namespace and not allow us to overhead his logic. Another pro of this technique is that we can use same namespace in several files. Anyway, if a namespace was not declared before in application, we simply assign the variable with empty object and thereby create new namespace.</p><h4>Object constructors</h4><p>As we know in JavaScript we can create a new object in two ways. First way is to use the <em>new</em> keyword:</p><pre>var rectangle = new Object();<br>rectangle.a = 10;<br>rectangle.b = 5;<br>rectangle.getPerimeter = function() {<br> return 2*(this.a + this.b);<br>}</pre><p>Second way is to use object literal that is commonly used way. The big pro of this way is that you can encapsulate a creation of an object. The previous example can be rewritten as follows:</p><pre>var rectangle = {<br> a: 10,<br> b: 5,<br> getPerimeter: function() {<br> return 2*(this.a + this.b);<br> }<br>}</pre><p>Two ways above show us how to create a simple JavaScript object. It’s fine, but what if I want to create number of rectangles in my application? Do I need to duplicate that code for every rectangle instance? The answer, of course, is no. We can use a constructors. Two previous examples can be rewritten with constructor as follows:</p><pre>function Rectangle(a, b) {<br> this.a = a;<br> this.b = b;<br> this.getPerimeter = function() {<br> return 2*(this.a + this.b);<br> };<br>}<br>var rectangle = new Rectangle(10,5);</pre><p>Constructors in ECMAScript are used to create specific types of objects. Constructors provide us with ability to create new object instances in simple way. More of that, every instance that created with constructor can be identified which type is he.</p><pre>function Rectangle(a, b) {<br> this.a = a;<br> this.b = b;<br> this.getPerimeter = function() {<br> return 2*(this.a + this.b);<br> };<br>}<br>var rectangle = new Rectangle(10,5);<br>alert(rectangle.constructor == Rectangle); // alerts true<br>alert(rectangle instanceof Rectangle); //alerts true</pre><p><a href="http://jsfiddle.net/dima_k/z97cn/2/">Try it in jsFiddle</a></p><p>The example above shows that we can identify type of the object in two ways. By checking his constructor property and by using <em>instanceof </em>operator. During object creation the constructor property is automatically assigned with a reference to constructor function. But the constructor is not placed on the object itself. It’s placed on a prototype of the object. We can check object prototype by inspecting an object by Dev Tools or by accessing a __proto__ property of an object in Chrome, Safary and Firefox.</p><p>So until now we can understand 3 important things:</p><ol><li>All variables and functions are placed on Global Object</li><li>To prevent name collision we can use a namespaces</li><li>We need to use a constructors to create specific types of objects</li></ol><p>But when we look at constructors we can understand that it is just regular JavaScript function and as such, when placed on global scope, can be easily overwritten by another function with same name. So we will want to place it under namespace as well. It can look like follows:</p><pre>var namespace = namespace || {};<br>namespace.Rectangle = function(a, b) {<br> this.a = a;<br> this.b = b;<br> this.getPerimeter = function() {<br> return 2*(this.a + this.b);<br> };<br>}<br>var rectangle = new namespace.Rectangle(10,5);</pre><p>It’s good, but when we will expect a constructor of rectangle, we will see that there is anonymous function so the prototype will be of regular Object type.</p><figure><img alt="proto" src="https://cdn-images-1.medium.com/max/584/0*cLoeBmVhAdXyQZp9.png" /></figure><p>What we want, is to be able with a namespace to look at prototype and to identify that this object is instance of Rectangle. I still can use <em>instanceof </em>operator and it’s smart enough to say me that is a Rectangle, but as a developer I will like to see it in my DevTools as well and to know that object prototype is Rectangle. To achieve that, we can use a next trick:</p><pre>var namespace = namespace || {};<br>namespace.Rectangle = function &lt;strong&gt;Rectangle&lt;/strong&gt;(a, b) {<br> this.a = a;<br> this.b = b;<br> this.getPerimeter = function() {<br> return 2*(this.a + this.b);<br> };<br>}<br>var rectangle = new namespace.Rectangle(10,5);</pre><p>What I did is simply give name to a constructor function. Now we can see that our object constructor is under namespace and we still can see that object prototype is Rectangle:</p><figure><img alt="proto2" src="https://cdn-images-1.medium.com/max/584/0*M0Rjw3BW1XHsCwJi.png" /></figure><p>Using this way take care of IE &lt;= 8.</p><p>Thanks</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d48f7e20bdb9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prevent scrolling of parent element with JavaScript]]></title>
            <link>https://medium.com/@forforeach/prevent-scrolling-of-parent-element-with-javascript-3304b257b045?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/3304b257b045</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[frontend]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Tue, 16 Jul 2013 13:16:30 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:08.639Z</atom:updated>
            <content:encoded><![CDATA[<p>In my current project I was asked to avoid scrolling of parent element, when reaching the end of the scroll of inner element.</p><p>It sounds like very simple task. Not when you want to implement it cross browser. So, I searched a little and found<a href="https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-17/handling-mousewheel-events"> next example</a> from the book: “JavaScript: The Definitive Guide”. By the way it’s very good book to start with when you want to learn JavaScript basics.</p><p>So, based on this example<a href="http://jsfiddle.net/dima_k/5mPkB/1/"> here is jsFiddle</a> with my solution for a problem.</p><p>Enjoy</p><figure><img alt="javascript-book" src="https://cdn-images-1.medium.com/max/176/0*XZJQkYt4TbPLmWCO.jpg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3304b257b045" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KnockoutJS simple truncate text binding handler]]></title>
            <link>https://medium.com/@forforeach/knockoutjs-simple-truncate-text-binding-handler-bd744cdc7c6e?source=rss-46c67831b471------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd744cdc7c6e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[knockoutjs]]></category>
            <dc:creator><![CDATA[Dima Kuzmich]]></dc:creator>
            <pubDate>Mon, 24 Jun 2013 09:07:05 GMT</pubDate>
            <atom:updated>2017-08-22T09:52:11.684Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello everyone,</p><p>On my current project I needed to truncate text in some titles. Since we’re using KnockoutJS, I decided to write custom binding handler. And so I did.</p><p>Custom binding handlers are great solution, that provides you with ability to control how your observables interact with DOM elements. In addition you encapsulate your custom behaviors, thus they became reusable. It’s very useful when you use third party widgets that need to be updated all the time when observable values are updated.</p><p>So here is an example of my truncate binding handler:</p><pre>ko.bindingHandlers.truncatedText = {<br> update: function (element, valueAccessor, allBindingsAccessor) {<br> var originalText = ko.utils.unwrapObservable(valueAccessor()),<br> // 10 is a default maximum length<br> length = ko.utils.unwrapObservable(allBindingsAccessor().maxTextLength) || 20,<br> truncatedText = originalText.length &amp;gt; length ? originalText.substring(0, length) + “…” : originalText;<br> // updating text binding handler to show truncatedText<br> ko.bindingHandlers.text.update(element, function () {<br> return truncatedText; <br> });<br> }<br>};</pre><p>You can try it <a href="http://jsfiddle.net/dima_k/bZEQM/1/">here</a></p><p>Use this binding handler and use binding handlers in general.</p><p>Enjoy.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd744cdc7c6e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>