Skip to content

Instantly share code, notes, and snippets.

View beyond-code-github's full-sized avatar

Pete Smith beyond-code-github

View GitHub Profile
@beyond-code-github
beyond-code-github / arraysubscriptions.js
Last active August 29, 2015 13:55
Suscribing to array changes using the new arraychange subscriptions in knockout 3.0
//....
//....
target.getChanges = function () {
var result = {
added: target.added(),
removed: target.removed()
};
return result;
@beyond-code-github
beyond-code-github / changetrackedarray.js
Last active August 29, 2015 13:55
Skeleton change tracked array extender for knockout.kj
ko.extenders.trackArrayChange = function (target, track) {
if (track) {
target.isDirty = ko.observable(false);
target.added = ko.observableArray([]);
target.removed = ko.observableArray([]);
var addItem = function (item) {
//...
};
@beyond-code-github
beyond-code-github / efficientiteratortests.cs
Created January 21, 2014 22:09
Efficient iterator specifications
public class ItemA
{
public string Identifier { get; set; }
}
public class ItemB
{
public string Name { get; set; }
}
@beyond-code-github
beyond-code-github / efficientiterator.cs
Last active January 4, 2016 01:38
Efficient Iterator sample
public class EfficientIterator<TOne, TTwo>
{
private Action<TOne, TTwo> always = (one, two) => { };
private Action<TOne, TTwo> match = (one, two) => { };
private Action<TOne> oneOnly = (one) => { };
private Action<TTwo> twoOnly = (two) => { };
private readonly Func<TOne, TTwo, int> compare;
public Action<TOne, TTwo> Always { set { this.always = value; } }
@beyond-code-github
beyond-code-github / compounditeration.cs
Created January 21, 2014 21:23
Example of compound iteration when performing validation against loosely typed data
public List<string> Validate(IDictionary<string, object> data, List<Field> mandatoryFields)
{
var errors = new List<string>();
foreach (var field in mandatoryFields)
{
if (!data.Keys.Contains(field.Identifier))
{
errors.add(String.Format("The {0} field is required", field.Identifier);
}
@beyond-code-github
beyond-code-github / complexexamplemultilevel.js
Last active January 3, 2016 00:58
Example use case demonstrating complex object change tracking at multiple levels
var viewModel = {
Name: ko.observable("Pete"),
Age: ko.observable(29),
Skills: ko.observable({
Tdd: ko.observable(true),
Knockout: ko.observable(true),
ChangeTracking: ko.observable(false),
Languages: ko.observable({
Csharp: ko.observable(false),
Javascript: ko.observable(false)
@beyond-code-github
beyond-code-github / highlight.js
Created January 12, 2014 15:07
Highlighted portion of example illustrating how we deal with wholesale replacement of a complex object within an observable
if ((typeof obj == "object") && (obj !== null)) {
if (target.hasValueChanged()) {
return ko.mapping.toJS(obj);
}
return getChangesFromModel(obj);
}
@beyond-code-github
beyond-code-github / complexchangetracking.js
Last active January 3, 2016 00:59
Changes required to trackChange and getChangesFromModel methods to support change tracking as well as detection
ko.extenders.trackChange = function (target, track) {
if (track) {
// ...
// ...
if (!target.getChanges) {
target.getChanges = function (newObject) {
var obj = target();
if ((typeof obj == "object") && (obj !== null)) {
if (target.hasValueChanged()) {
@beyond-code-github
beyond-code-github / complexdetectionexample.js
Last active January 3, 2016 00:49
Example of change tracking behavior after applying complex object detection
var viewModel = {
Name: ko.observable("Pete"),
Age: ko.observable(29),
Skills: {
Tdd: ko.observable(true),
Knockout: ko.observable(true),
ChangeTracking: ko.observable(false),
},
Occupation: ko.observable("Developer")
};
@beyond-code-github
beyond-code-github / complextracker.js
Last active January 3, 2016 00:49
Modified track changes knockout extended that can handle detection of changes to complex objects
var traverseObservables = function (obj, action) {
ko.utils.arrayForEach(getObjProperties(obj), function (observable) {
if (observable && observable.value && !observable.value.nodeType && ko.isObservable(observable.value)) {
action(observable);
}
});
};
ko.extenders.trackChange = function (target, track) {
if (track) {