From 8b9521f61d41703dd327a4777ee8cbc609beac4c Mon Sep 17 00:00:00 2001 From: Tristan Slominski Date: Sat, 7 Jan 2012 13:04:52 -0600 Subject: [PATCH] Implemented fix for #55, accept data-bind variables in any order --- agility.js | 53 +++++++++++++++++++++++---------------------- test/public/core.js | 10 +++++++++ 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/agility.js b/agility.js index 2d876ae..009bf46 100644 --- a/agility.js +++ b/agility.js @@ -423,41 +423,42 @@ return this; }, // render - // Parse data-bind string of the type '[attribute][=] variable[, attribute[=] variable ]...' - // If the variable is not an attribute, it must be first in the list, - // all following pairs in the list are assumed to be attributes + // Parse data-bind string of the type '[attribute][=] variable[, [attribute][=] variable ]...' + // If the variable is not an attribute, it must occur by itself + // all pairs in the list are assumed to be attributes // Returns { key:'model key', attr: [ {attr : 'attribute', attrVar : 'variable' }... ] } _parseBindStr: function(str){ var obj = {key:null, attr:[]}, pairs = str.split(','), regex = /([a-zA-Z0-9_\-]+)(?:[\s=]+([a-zA-Z0-9_\-]+))?/, + keyAssigned = false, matched; if (pairs.length > 0) { - matched = pairs[0].match(regex); - // [ "attribute variable", "attribute", "variable" ] - // or - // [ "variable", "variable", undefined ] - // in some IE it will be [ "variable", "variable", "" ] - // or - // null - if (matched) { - if (typeof(matched[2]) === "undefined" || matched[2] === "") { - obj.key = matched[1]; - } else { - obj.attr.push({attr: matched[1], attrVar: matched[2]}); - } - } // if (matched ) - if (pairs.length > 1) { - for (var i = 1; i < pairs.length; i++) { - matched = pairs[i].match(regex); - if (matched) { - if (typeof(matched[2]) !== "undefined") { - obj.attr.push({attr: matched[1], attrVar: matched[2]}); + for (var i = 0; i < pairs.length; i++) { + matched = pairs[i].match(regex); + // [ "attribute variable", "attribute", "variable" ] + // or [ "attribute=variable", "attribute", "variable" ] + // or + // [ "variable", "variable", undefined ] + // in some IE it will be [ "variable", "variable", "" ] + // or + // null + if (matched) { + if (typeof(matched[2]) === "undefined" || matched[2] === "") { + if (keyAssigned) { + throw new Error("You may specify only one key (" + + keyAssigned + " has already been specified in data-bind=" + + str + ")"); + } else { + keyAssigned = matched[1]; + obj.key = matched[1]; } - } // if (matched) - } // for (pairs.length) - } // if (pairs.length > 1) + } else { + obj.attr.push({attr: matched[1], attrVar: matched[2]}); + } + } // if (matched) + } // for (pairs.length) } // if (pairs.length > 0) return obj; diff --git a/test/public/core.js b/test/public/core.js index d925dfe..46c81d9 100644 --- a/test/public/core.js +++ b/test/public/core.js @@ -75,6 +75,16 @@ equals( obj.view.$( 'span' ).last().attr( 'name' ), 'Joe Doe', 'format as expected'); equals( obj.view.$( 'span' ).last().attr( 'last-color' ), 'Blue', 'format as expected'); }); + + test("Two arguments (model object, view string) with multiple attribute bindings random order", function () { + var obj = $$({ + first: 'Joe', + name: 'Joe Doe' + }, '
' ); + validateObject( obj ); + equals( obj.view.$().text(), 'Joe', 'format as expected'); + equals( obj.view.$( 'span' ).first().attr( 'name' ), 'Joe Doe', 'format as expected'); + }); test("Three arguments (model object, view string, controller object)", function(){ var obj = $$({first:'Joe', last:'Doe'}, '
', {});