Skip to content

Commit

Permalink
Merge pull request #65 from tristanls/master
Browse files Browse the repository at this point in the history
Implemented fix for #55, accept data-bind variables/attributes in any order
  • Loading branch information
arturadib committed Jan 9, 2012
2 parents 0645fca + 8b9521f commit a2486c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
53 changes: 27 additions & 26 deletions agility.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions test/public/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}, '<div><span data-bind="name=name, first"/>' );
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'}, '<div><span data-bind="first"/><span data-bind="last"/></div>', {});
Expand Down

0 comments on commit a2486c8

Please sign in to comment.