if (window.Transistor === undefined) { window.Transistor = {}; } (function (transistor) { var Binder, ArrayBinding; ArrayBinding = { bindTo: function (array) { var H = (function () { return { clear: function () { for (;array.length !== 0;) { array.pop(); } }, indexOfId: function (id) { for (var i = 0; i < array.length; i += 1) { if (array[i].id === id) { return i; } } throw new Error("unknown id: " + id); } }; }()), set = function (collection) { H.clear(); if (collection !== undefined) { for (var i = 0 ; i < collection.length; i += 1) { array[i] = collection[i]; } } }; return { init: set, set: set, insert: function (entry) { if (entry !== undefined) { array.push(entry); } }, update: function (id, entry) { if (id === undefined) { throw new Error("undefined id"); } array[H.indexOfId(id)] = entry; }, remove: function (id) { array.splice(H.indexOfId(id), 1); } }; } }; Binder = (function () { return function (binding) { var validate, target; if (Object.prototype.toString.call( binding ) === '[object Array]') { target = ArrayBinding.bindTo(binding); } else { target = binding; } validate = (function () { return { collection: function (args) { if (args.collection === undefined) { throw new Error("undefined collection"); } }, entry: function (args) { if (args.entry === undefined) { throw new Error("undefined entry"); } }, id: function (args) { if (args.id === undefined) { throw new Error("undefined id"); } } }; }()); return function (event, args) { if (args === undefined) { throw new Error("undefined arguments"); } switch (event) { case 'init': target.init(args); break; case 'set': validate.collection(args); target.set(args.collection); break; case 'insert': validate.entry(args); target.insert(args.entry); break; case 'update': validate.id(args); validate.entry(args); target.update(args.id, args.entry); break; case 'remove': validate.id(args); target.remove(args.id); break; } }; }; }()); transistor.Binder = Binder; }(window.Transistor));