Skip to content

Instantly share code, notes, and snippets.

@snit-ram
Last active December 11, 2015 21:58
Show Gist options
  • Save snit-ram/4666222 to your computer and use it in GitHub Desktop.
Save snit-ram/4666222 to your computer and use it in GitHub Desktop.
Combined List Projection. Combines items with groups
// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
"use strict";
WinJS.Binding.optimizeBindingReferences = true;
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize
// your application here.
} else {
// TODO: This application has been reactivated from suspension.
// Restore application state here.
}
args.setPromise(WinJS.UI.processAll());
}
};
app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended. Save any state
// that needs to persist across suspensions here. You might use the
// WinJS.Application.sessionState object, which is automatically
// saved and restored across suspension. If you need to complete an
// asynchronous operation before your application is suspended, call
// args.setPromise().
};
app.start();
WinJS.Namespace.define('Test', {
List: new WinJS.Binding.List([
{ name: 'Mário' },
{ name: 'Luigi' },
{ name: 'Alganet' },
{ name: 'Marcelo' }
])
});
Test.Filter = /Lu/;
Test.Filtered = Test.List.createFiltered(function (item) {
if (!Test.Filter) {
return true;
}
return item.name.match(Test.Filter);
});
Test.CombinedList = WinJS.Class.derive(WinJS.Binding.List, function (list, groupKey, groupData) {
WinJS.Binding.List.call(this, []);
var that = this;
this._origList = list.createGrouped(groupKey, groupData);
this._origList.groups.addEventListener('reload', function () {
that.notifyReload();
});
}, {
length: {
get: function () {
return this._origList.length + this._origList.groups.length;
}
},
getItem: function (key) {
if (key >= this._origList.length) {
return this._origList.groups.getItem(key - this._origList.length);
}
return this._origList.getItem(key);
},
getItemFromKey: function (key) {
return this._origList.getItemFromKey(key) || this._origList.groups.getItemFromKey(key);
}
});
Test.Combined = new Test.CombinedList(Test.Filtered, function (x) {
return x.name.charAt(0);
}, function (x) {
return {
name: x.name.charAt(0),
isGroup: true
}
}).createSorted(function (a, b) {
return a.name.localeCompare(b.name);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment