build/src/transistor-radio.js in transistor-0.1.3 vs build/src/transistor-radio.js in transistor-0.1.4

- old
+ new

@@ -157,15 +157,15 @@ if (window.Transistor === undefined) { window.Transistor = {}; } (function (transistor) { - var Binder = (function () { - return function (array) { - var H, set, insert, update, remove; + var Binder, ArrayBinding; - H = (function () { + ArrayBinding = { + bindTo: function (array) { + var H = (function () { return { clear: function () { for (;array.length !== 0;) { array.pop(); } @@ -177,84 +177,101 @@ return i; } } throw new Error("unknown id: " + id); - }, + } + }; + }()); - expectCollection: function (args) { + return { + set: function (collection) { + H.clear(); + + if (collection !== undefined) { + for (var i = 0 ; i < collection.length; i += 1) { + array[i] = collection[i]; + } + } + }, + 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"); } }, - expectEntry: function (args) { + entry: function (args) { if (args.entry === undefined) { throw new Error("undefined entry"); } }, - expectId: function (args) { + id: function (args) { if (args.id === undefined) { throw new Error("undefined id"); } } }; }()); - set = function (collection) { - H.clear(); - - if (collection !== undefined) { - for (var i = 0 ; i < collection.length; i += 1) { - array[i] = collection[i]; - } - } - }; - - 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); - }; - return function (event, args) { if (args === undefined) { throw new Error("undefined arguments"); } switch (event) { case 'init': - set(args); + target.set(args); break; case 'set': - H.expectCollection(args); - set(args.collection); + validate.collection(args); + target.set(args.collection); break; case 'insert': - H.expectEntry(args); - insert(args.entry); + validate.entry(args); + target.insert(args.entry); break; case 'update': - H.expectId(args); - H.expectEntry(args); - update(args.id, args.entry); + validate.id(args); + validate.entry(args); + target.update(args.id, args.entry); break; case 'remove': - H.expectId(args); - remove(args.id); + validate.id(args); + target.remove(args.id); break; } }; }; }());