Git Product home page Git Product logo

knockout-winjs's Introduction

knockout-winjs

Adapter for KnockoutJS and WinJS, this code facilitates usage of WinJS UI controls in a Knockout application. It achieves this by creating bindings for the various controls which allow them to show up in Knockout data-bind attributes like:

The current rating is: <span data-bind="text: userRating"></span><br/>
<div data-bind="winRating: {userRating: userRating}"></div>

How to use this in your Knockout project?

Just make sure to include WinJS, and then include the adapter.

<link href="winjs/css/ui-dark.css" rel="stylesheet" />
<script src="winjs/js/base.js"></script>
<script src="winjs/js/ui.js"></script>
<script src="js/knockout-winjs.js"></script>

Note: this adapter library has only been tested against Knockout 3.3+.

Examples of control usage

AppBar and AppBarCommand

<!-- Shows up on the bottom of the screen, use right-click or touch edgy gesture to show -->
<div data-bind="winAppBar: {placement: 'bottom'}">
    <button data-bind="winAppBarCommand: {label: 'AppBarButton', type: 'button', icon:'add'}"></button>
</div>

AutoSuggestBox

<div data-bind="winAutoSuggestBox"></div>

BackButton

<!-- Won't show up unless you use WinJS.Navigation stack -->
<button data-bind="winBackButton"></button>

ContentDialog

<div data-bind="winContentDialog"></div>

DatePicker

<div data-bind="winDatePicker: {current: date}"></div>

FlipView

<div class="flipView" data-bind="winFlipView: {itemTemplate: templateName, itemDataSource: flipViewArray}"></div>
<script type="text/html" id="flipViewTemplate">
  <div>
    <span>Name: </span><span data-bind="text: text"></span>
  </div>
  <div>
    <span>Rating: </span><span data-bind="text: rating"></span>
  </div>
</script>

Flyout

<button id="flyoutAnchor">Show a flyout!</button>
<div data-bind="winFlyout: {anchor: '#flyoutAnchor', placement: 'right'}">
    <div>This is a flyout</div>
</div>

Hub and HubSection

<div class="hub" data-bind="winHub">
  <div class="hubSection" data-bind="winHubSection: {header: 'Section1'}"></div>
  <div class="hubSection" data-bind="winHubSection: {header: 'Section2'}"></div>
  <div class="hubSection" data-bind="winHubSection: {header: 'Section3'}"></div>
</div>

ItemContainer

<!-- shown here used within a ko foreach -->
<div data-bind="foreach: ratings">
  <div class="item" data-bind="winItemContainer: {swipeBehavior: 'select', swipeOrientation: 'horizontal', selected: selected}">
    <div class="content"><span>Person: </span><span data-bind="text: text"></span>&nbsp;
    <span>Rating: </span><span data-bind="text: rating"></span>&nbsp;
    <span>Selected: </span><span data-bind="text: selected"></span></div>
  </div>
</div>

ListView

<div class="listView" data-bind="winListView: {itemTemplate: 'listViewItemTemplate', itemDataSource: listViewArray, layout: {type: WinJS.UI.GridLayout}}"></div>
<script type="text/html" id="listViewItemTemplate">
  <div class="listViewItem">
    <div>
      <span>Name: </span><span data-bind="text: text"></span>
    </div>
    <div>
      <span>Rating: </span><span data-bind="text: rating"></span>
    </div>
  </div>
</script>

Menu and MenuCommand

<button id="menuAnchor">Show a menu!</button>
<div data-bind="winMenu: {anchor: '#menuAnchor', placement: 'right' }">
  <button data-bind="winMenuCommand: {id: 'menu2', label: 'Menu2', type: 'button'}"></button>
  <hr data-bind="winMenuCommand: {type: 'separator'}" />
  <button data-bind="winMenuCommand: {id: 'menu3', label: 'Menu3', type: 'button'}"></button>
</div>

Pivot

<div data-bind="winPivot, event: { itemanimationend: itemAnimationEndHandler }">
    <div data-bind="winPivotItem: { header: 'Header1' }">Item1</div>
    <div data-bind="winPivotItem: { header: 'Header2' }">Item2</div>
</div>

Rating

The current rating is: <span data-bind="text: userRating"></span><br/>
<div data-bind="winRating: {userRating: userRating}"></div>

SemanticZoom

<div data-bind="winSemanticZoom">
    <div data-bind="winListView: { itemDataSource: zoomedInSource.dataSource, groupDataSource: zoomedInSource.groups.dataSource }"></div>
    <div data-bind="winListView: { itemDataSource: zoomedOutSource.dataSource }"></div>
</div>

SplitView

<div data-bind="winSplitView">
    <div class="paneNode">
        SplitView pane area
        <button data-bind="winSplitViewCommand: {label: 'Home', icon: 'home', oninvoked: goToHome}"></button>
        <button data-bind="winSplitViewCommand: {label: 'Settings', icon: 'settings', oninvoked: goToSettings}"></button>
    </div>
    <div class="aContentNode">Text for the SplitView content area</div>
    <div class="anotherContentNode">More text for the SplitView content area</div>
</div>

SplitViewPaneToggle

<button data-bind="winSplitViewPaneToggle: { splitView: splitViewElement }"></button>

TimePicker

<div data-bind="winTimePicker: {current: time}"></div>

ToggleSwitch

<div data-bind="winToggleSwitch: {checked: toggleValue, title: toggleTitle, labelOff: toggleLabelOff, labelOn: 'world'}"></div>

ToolBar

<div data-bind="winToolBar">
    <button data-bind="winCommand"></button>
    <button data-bind="winCommand"></button>
</div>

Tooltip

<div data-bind="winTooltip: {contentElement: '#tip', infotip: true}">
  <div>Hover for infoTip</div>
</div>
<div style="display: none">
  <div id="tip">
    <div>Tooltip only allows one time binding of with contentElement:</div>
    <div data-bind="text: toolTipContent"></div>
  </div>
</div>

knockout-winjs's People

Contributors

amazingjaze avatar jseanxu avatar yildirimcagri-msft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

knockout-winjs's Issues

Update for WinJS 4.0

This appears to still be built against a much outdated version of WinJS. Would love to use this.

winSplitViewCommand with PNG Icon

Hey there,
How can I use png icons in the SplitView?

I tried with:

  <div class="nav-command" data-bind="winSplitViewCommand:{label: Title(), icon:FontIcon()}"></div>

Where FontIcon() is a KnockoutObservable<string> with a url for png or just the icons enum name like "home" for the rest.

Thanks for your time!

winListView binding doesn't correctly cleanup when user calls ko.cleanNode.

After calling ko.cleanNode on an element with an active winListView binding, the observable array passed as itemDataSource still has _winKoChangesSubscription and _winKoBindingList. If you attempt to rebind this array, to another winjs listview, the observable array and WinJS ListBinding will never synchronize because the subscription to the old ListBinding is still active and on the array.

A proposed fix for this is to call ko.utils.domNodeDisposal.addDisposeCallback on the element with the binding to detect a call to cleanNode. In the callback, you can dispose of the subscription and delete the _winKo members from the observable array.

I'm effectively doing this cleanup in my control's dispose handler as a workaround.

winAppBar with observable Array for winAppBarCommands

Hi,

I'm having some issues trying to make the appbar Commands observables with a KnockoutObservableArray .

The best approach was something like this:

<div data-bind="winAppBar:{'placement':commandBar.placement}">
                        <!-- ko foreach:commandBar.commands()-->
                        <button data-bind="winAppBarCommand:{'label':label,'icon':icon,'tooltip':tooltip,'section':section}" button>
                        <!--/ko-->
                    </div>

But it has a problem when navigating between pages and the observable array changes, that makes the commads appears on the virtual ko tags, makes sense because kncokout-winjs doesn't has a bindingListWatch or something else.

Also I tried using WinJS Toolbar control, it has a "data" option that can use WinJS.Binding.List but in knockout-winjs I didn't find it, are there plans to include it?

Thanks!

$root.Function not working on button inside listview item

Having a problem with event handler for buttons inside list view item.
Everything seems to be working good, but once you declare a button/anchor inside the template, the event is not bonded to the element.

 <div class="listView" data-bind="winListView: {itemDataSource: itemlist, itemTemplate: 'listItemTemplate', layout: {type: WinJS.UI.GridLayout}}">
   <progress class=" win-ring win-medium"></progress>
 </div>
 <script type="text/html" id="listItemTemplate">
   <div class="listViewItem">

     <div data-bind="css: cssClass">
       <div class="row">
         <div class="large-2 columns">
           <a id="removeFromList" data-bind="click: $root.removeItem" class="tiny"><i class="fi-x"></i></a>
         </div>
         <div class="large-10 columns">
           <img data-bind="style: { backgroundImage: urlProfilePic }" />
           <p data-bind="text: fullname"></p>
         </div>
       </div>
     </div>

   </div>
 </script>

Here is a full HTML example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.0/css/foundation.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.4.0/css/ui-light.css" />

</head>
<body>

     <h5>Listview Example with Knockout</h5>
     <button type="button" data-bind="click: $root.addItem" class="hollow button">add item</button>

     <div id="itemList" class="listView" data-bind="winListView: {itemDataSource: itemlist, itemTemplate: 'listItemTemplate', layout: {type: WinJS.UI.GridLayout}}">
       <progress class=" win-ring win-medium"></progress>
     </div>
     <script type="text/html" id="listItemTemplate">
       <div id="eachitem" class="listViewItem">

         <div data-bind="css: cssClass">
           <div class="row">
               <img data-bind="style: { backgroundImage: urlPic }" />
               <p data-bind="text: fullname"></p>
               <a data-bind="click: $root.removeItem" class="tiny button action win-interactive" 
                    data-win-control="WinJS.UI.AppBarCommand">remove item</a>
           </div>
         </div>

       </div>
     </script>

     <h5>Listview Example without Knockout, using an array only</h5>

     <div id="itemviewtemplate" data-win-control="WinJS.Binding.Template">
         <div data-win-bind="className: cssClass">
           <div class="row">
                <p data-win-bind="textContent: fullname"></p>

                <button class="action win-interactive" data-win-control="WinJS.UI.AppBarCommand"
                        data-win-options="{id:'removeFromList', tooltip:'remove item', icon: 'delete'}"
                        data-win-bind="this['data-itemname'] : fullname WinJS.Binding.setAttribute"
                        data-displaytype="button"
                        type="button"></button>            

           </div>
         </div>  
     </div>
     <div id="otherlist"></div>

    <script type="text/javascript" src="http://code.jquery.com/jquery-2.2.0.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.0/js/foundation.min.js"></script> 

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.4.0/js/base.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/winjs/4.4.0/js/ui.js"></script>

    <script type="text/javascript" src="knockout-winjs.js"></script>

    <script type="text/javascript" >

        var ItemModel = function(items) {
            var self = this;
            self.itemlist = ko.observableArray(items);

            self.addItem = function() {
                self.itemlist.push({
                    fullname: "new item " + (1+self.itemlist().length),
                    cssClass: "item-class" + (1+self.itemlist().length),
                    urlPic: ""
                });
            };

            self.removeItem = function(item) {
                alert("remove item from list " + item.fullname);
                self.itemlist.remove(item);
            };

        };

        var items = [
            { fullname: "Tall Hat", cssClass: "item-class1", urlPic:""},
            { fullname: "Long Cloak", cssClass: "item-class2", urlPic:""},
            { fullname: "Steve Yes", cssClass: "item-class3", urlPic:""},
            { fullname: "Mr Brown", cssClass: "item-class4", urlPic:""},
            { fullname: "Bob Smith", cssClass: "item-class5", urlPic:""}
        ];

        var viewModel = new ItemModel(items);           

        $( document ).ready(function() {

            // Starts the application
            var app = WinJS.Application;
            app.addEventListener("activated", Initiate, false);
            app.start();    

        });

        function Initiate(){

            // solutions used without knockout
            bindList();
            $(document).foundation();
            ko.applyBindings(viewModel)

        }

        function bindList(){

             WinJS.UI.processAll(document.getElementById("otherlist"));
             var viewDiv = document.getElementById("otherlist");

             var itemDiv = document.getElementById("itemviewtemplate");
             var itemTemplate = new WinJS.Binding.Template(itemDiv);             

             var list = new WinJS.Binding.List(items);

             var listView = new WinJS.UI.ListView(viewDiv, null);
             var layout = new WinJS.UI.GridLayout();
             listView.layout = layout;  
             // remove the line below and the listview title will also be clickable
             listView.selectionMode = WinJS.UI.SelectionMode.none;           
             listView.itemDataSource = list.dataSource;
             listView.oniteminvoked = itemView;
             listView.itemTemplate = itemsItemTemplateRenderer;
             listView.forceLayout();

             WinJS.Utilities.markSupportedForProcessing(itemsItemTemplateRenderer);

        }

        function itemView(ev) {

            var viewDiv = ev.currentTarget;
            if (viewDiv === null || viewDiv === undefined) { return; };
            var view = viewDiv.winControl;
            if (view === null || view === undefined) { return; };

            view.itemDataSource.itemFromIndex(ev.detail.itemIndex).then(
              function (item) {
                alert("loading item " + item.data.fullname);
              });

        }

        function itemsItemTemplateRenderer(itemPromise) {
            var self = this;
            var itemDiv = document.getElementById("itemviewtemplate");
            var container = document.createElement('div');

            return itemPromise.then(
              function onitem(item) {
                return itemDiv.winControl.render(item.data, container);
              }).then(
              function onrendercomplete(c) {
                // register event listener
                var button = c.querySelector('#removeFromList');
                button.onclick = removeItem.bind(self);
                return container;
              });

        }

        function removeItem(ev){

            alert("remove item from list " + $(ev.currentTarget).data("itemname"));

        }

    </script>

</body>
</html>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.