Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Telerik School Academy ASP.NET MVC.
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Telerik School Academy ASP.NET MVC.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
With Mocha and Karma Telerik Academy Telerik Software Academy.
C# Fundamentals – Part I
NoSQL Concepts, Redis, MongoDB, CouchDB
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
ORM Concepts, Entity Framework (EF), DbContext
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
The past, the present, the future Telerik School Academy HTML, CSS and JavaScript.
Doncho Minkov Telerik Software Academy academy.telerik.com Technical Trainer Next Generation for Desktop Applications.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Presentation transcript:

Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net

2  Data Binding  Concepts  WinJS API for bindings  WinJS Binding Basics  Attributes, observables, binding data and styles  Binding converters  Using templates

Concepts, Advantages, WinJS APIs

4  Data binding connects UI and business logic  The UI layer describes how it visualizes data  Properties from sources mapped to UI elements  The Business layer describes the data itself  Defines objects and processes  The Data Binding API handles data updates  Visualizes data from business logic in the UI  Takes UI data & updates object properties  In good implementations, business logic has no idea there is UI

 Advantages to Data Binding  Mechanism to detach UI from business logic  Developer writes less (none) UI interaction code  UI design is independent of development  Data sources can be easily mocked and simulated  Disadvantages  A bit slower than direct UI interaction  Overhead in detecting data changes 5

 WinJS supports Data binding  Through WinJS.Binding namespace  One-way  Changes to JS objects trigger changes to UI  UI changes DON'T update JS objects  Provides attributes to bind UI controls  HTML elements  WinJS controls 6

Element attributes, bindable objects, data & style binding

 WinJS bindings use data attributes plus JS code to bind HTML elements  data-win-bind – object to element properties  Format:  Example:  WinJS.Binding.processAll()  Takes a root element and binding context object  Traverses elements and binds them with context 8 "elementProperty1: objectProperty1; elementProperty2: objectProperty2.subProperty" elementProperty2: objectProperty2.subProperty" WinJS.Binding.processAll(document.body, {text: 'hi'}) Same as calling with null

 More detail on WinJS.Binding.processAll():  Parses & applies data-win-bind attributes  No data binding occurs if not called  Second parameter defines object context  Determines which object is searched for the properties referenced in data-win-bind  When called with no parameters:  Traverses the entire DOM and applies bindings  Only bindings to global objects will work 9

 Notes on binding  Always have WinJS.Binding.optimizeBindingReferences  Prevents memory leaks from binding  Never bind to element IDs  Have a collection of your objects and bind them to the document  E.g. a namespace with all objects to display  Access appropriate object for element through "." operator 10

Live Demo

Enabling automatic updates to UI on object changes

 Binding simple objects doesn't enable updates  WinJS needs notification of property changes  Property changes typically handled in setters  Setter gives value then calls an API's function  Mixins in WinJS.Binding enable this on objects  Several methods to convert JS objects to WinJS observables in WinJS.Binding:  as() – object to observable  define() – object to constructor for observables  Note: work properly ONLY with DTOs DTO 13

 Using WinJS.Binding.as()  Takes an object to make observable  Returns the observable object  Keeps all members and their values  All members become observable properties  Methods and properties don't bind properly  Changes to the object are reflected in its binding 14 var personObservable = WinJS.Binding.as({ name: 'Joe', age: 21}); name: 'Joe', age: 21}); WinJS.Binding.processAll(null, personObservable); setInterval(function(){personObservable.age++;}, 1000)

 Using WinJS.Binding.define()  Takes a normal object template and makes a constructor for such observable objects  Returns the object constructor  All instances become observable properties  Methods & properties don't bind properly 15 var ObservablePerson = WinJS.Binding.define({ name: "", age: 0}); name: "", age: 0}); var person = new ObservablePerson(); WinJS.Binding.processAll(null, person); person.name = "Joe", person.age = 21; setInterval(function(){personObservable.age++;}, 1000)

Live Demo

Translating object info into UI appropriate info

 Bound property values often need conversion  To UI friendly data such as styles, HTML, etc.  To user-friendly texts (3.99 -> $3,99)  Not correct to have UI data in data objects  Such as styles, formatting, etc.  Remember: data should not be concerned with how it's displayed  Binding APIs usually support some type of Value Converters  Compute the representation of a value 18

 WinJS.Binding.converter() function  Receives the function to convert a value  Converter passes the function the value  Function should return converted value  Returns the converter function  Called inside a data-win-bind attribute value 19 <div data-win-bind='style.backgroundColor: frequency MyConverters.lightWavelengthToColor' frequency MyConverters.lightWavelengthToColor'</div> MyConverters.wavelengthToColor = WinJS.Binding.converters(function(wavelength){ /*return computed CSS color value*/...}); /*return computed CSS color value*/...});

Live Demo

Defining and reusing formatting for bindings

 Reusable declarative binding elements  WinJS.Binding.Template  Can contain any piece of HTML  Invisible; "instances" are visible when rendered  Can be rendered to the DOM several times with different bound objects  Two ways to initialize templates  Declaratively (needs WinJS.UI.processAll())  JS code, given element or HTML fragment URI  Both ways require some initial HTML code 22

 Creating Templates Declaratively  Write the HTML for the template  data-win-control='WinJS.Binding.Template' attribute in the container  Call WinJS.UI.processAll() to parse controls  Template container needs to be in the HTML code of the processed page to be parsed  WinJS will hide the control from the DOM  Access the element through container.winControl 23

Live Demo

 Creating Templates in JavaScript  Write the HTML for the template  Can be in an external file  Call constructor for WinJS.Binding.Template  Pass an HTML element containing the HTML for the template  Or pass an options object with a href member pointing to a HTML fragment for the template 25 var template = new WinJS.Binding.Template(element) var template = new WinJS.Binding.Template(null, { href: '/my-templates/sample-template.html}) href: '/my-templates/sample-template.html})

Live Demo

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

1. Read this article (and its previous parts) on efficient conversion of objects to WinJS observables this article (and its previous parts)this article (and its previous parts) 2. Implement a Repeater control. A repeater renders a collection of objects, according to an item template  The Repeater is initialized with a WinJS.Binding.Template  Each item the repeater renders uses the template  Items can be wrapped additionally in DIVs or SPANs  The Repeater has a render method, which takes a collection of objects and a DOM element to which to render them  If the DOM element is not provided, the repeater creates a new DIV and uses it, appending to document  The Repeater should place custom CSS styles to each item it renders, as well as to the container of the items

3. Write an application which enables users to create computer descriptions.  A computer description has all the properties of the computers from demos in this lecture + rating.  Rating is not defined on creation  The application should enable users to save descriptions and should reload all descriptions and visualize them when it starts  Use the repeater from the last  Enable rating computers in the visualized list 29