Skip to content

Instantly share code, notes, and snippets.

@kamranayub
Created December 1, 2012 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamranayub/4183235 to your computer and use it in GitHub Desktop.
Save kamranayub/4183235 to your computer and use it in GitHub Desktop.
Knockout View Binding Support for WinJS
//
// Via Kamranicus: http://kamranicus.com/Blog/Posts/59/using-knockout-bindings-in-your-winjs-application
//
// Include this before the rest of your app code (before default.js)
//
// <!-- WinJS references -->
// ...
//
// <script src="/scripts/knockout-2.2.js">
// <script src="/scripts/ko-winjs.js">
//
// <!-- Rest of your code -->
// ...
//
// *********************************
// WARNING: This is still an experimental idea. This possibly will be inefficient. Use at your own risk!
// *********************************
(function() {
"use strict";
//
// Knockout View Binding support
//
var superProcessAll = WinJS.Binding.processAll;
WinJS.Binding.processAll = function (rootElement, dataContext, skipRoot, bindingCache) {
if (dataContext) {
ko.applyBindings(dataContext, rootElement);
}
return superProcessAll.call(this, rootElement, dataContext, skipRoot, bindingCache);
};
})();
<!-- Now you can use both bindings! -->
<div id="item-tmpl" data-win-control="WinJS.Binding.Template">
<div class="item" data-bind="css: { toggledClass: someBoolProp }">
<h2 class="win-type-medium" data-bind="text: name"></h2>
<div class="item-logo"
data-win-bind="style.backgroundImage: logoUrl Converters.cssBackgroundify"
data-bind="visible: showLogo">
</div>
</div>
</div>
<div id="my-list"
data-win-control="WinJS.UI.ListView"
data-win-options="{ itemTemplate: select('#item-tmpl') }">
</div>
@kamranayub
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment