mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-18 23:50:23 +00:00
Support substring matching of bindings with repeater(). Closes #123
This commit is contained in:
parent
c048f0d8e8
commit
257e97a65f
4 changed files with 40 additions and 38 deletions
|
|
@ -294,3 +294,31 @@ function browserTrigger(element, type) {
|
|||
};
|
||||
})(_jQuery.fn);
|
||||
|
||||
/**
|
||||
* Finds all bindings with the substring match of name and returns an
|
||||
* array of their values.
|
||||
*
|
||||
* @param {string} name The name to match
|
||||
* @return {Array.<string>} String of binding values
|
||||
*/
|
||||
_jQuery.fn.bindings = function(name) {
|
||||
function contains(text, value) {
|
||||
return value instanceof RegExp ?
|
||||
value.test(text) :
|
||||
text && text.indexOf(value) >= 0;
|
||||
}
|
||||
var result = [];
|
||||
this.find('.ng-binding').each(function() {
|
||||
var element = new _jQuery(this);
|
||||
if (!angular.isDefined(name) ||
|
||||
contains(element.attr('ng:bind'), name) ||
|
||||
contains(element.attr('ng:bind-template'), name)) {
|
||||
if (element.is('input, textarea')) {
|
||||
result.push(element.val());
|
||||
} else {
|
||||
result.push(element.html());
|
||||
}
|
||||
}
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -111,6 +111,7 @@ angular.scenario.SpecRunner.prototype.addFutureAction = function(name, behavior,
|
|||
$document.elements = function(selector) {
|
||||
var args = Array.prototype.slice.call(arguments, 1);
|
||||
selector = (self.selector || '') + ' ' + (selector || '');
|
||||
selector = _jQuery.trim(selector) || '*';
|
||||
angular.foreach(args, function(value, index) {
|
||||
selector = selector.replace('$' + (index + 1), value);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -152,30 +152,16 @@ angular.scenario.dsl('using', function() {
|
|||
|
||||
/**
|
||||
* Usage:
|
||||
* binding(name) returns the value of a binding
|
||||
* binding(name) returns the value of the first matching binding
|
||||
*/
|
||||
angular.scenario.dsl('binding', function() {
|
||||
function contains(text, value) {
|
||||
return value instanceof RegExp ?
|
||||
value.test(text) :
|
||||
text && text.indexOf(value) >= 0;
|
||||
}
|
||||
return function(name) {
|
||||
return this.addFutureAction("select binding '" + name + "'", function($window, $document, done) {
|
||||
var elements = $document.elements('.ng-binding');
|
||||
for ( var i = 0; i < elements.length; i++) {
|
||||
var element = new elements.init(elements[i]);
|
||||
if (contains(element.attr('ng:bind'), name) ||
|
||||
contains(element.attr('ng:bind-template'), name)) {
|
||||
if (element.is('input, textarea')) {
|
||||
done(null, element.val());
|
||||
} else {
|
||||
done(null, element.html());
|
||||
}
|
||||
return;
|
||||
}
|
||||
var values = $document.elements().bindings(name);
|
||||
if (!values.length) {
|
||||
return done("Binding selector '" + name + "' did not match.");
|
||||
}
|
||||
done("Binding selector '" + name + "' did not match.");
|
||||
done(null, values[0]);
|
||||
});
|
||||
};
|
||||
});
|
||||
|
|
@ -243,16 +229,7 @@ angular.scenario.dsl('repeater', function() {
|
|||
|
||||
chain.column = function(binding) {
|
||||
return this.addFutureAction("repeater '" + this.label + "' column '" + binding + "'", function($window, $document, done) {
|
||||
var values = [];
|
||||
$document.elements().each(function() {
|
||||
_jQuery(this).find(':visible').each(function() {
|
||||
var element = _jQuery(this);
|
||||
if (element.attr('ng:bind') === binding) {
|
||||
values.push(element.text());
|
||||
}
|
||||
});
|
||||
});
|
||||
done(null, values);
|
||||
done(null, $document.elements().bindings(binding));
|
||||
});
|
||||
};
|
||||
|
||||
|
|
@ -262,13 +239,7 @@ angular.scenario.dsl('repeater', function() {
|
|||
var matches = $document.elements().slice(index, index + 1);
|
||||
if (!matches.length)
|
||||
return done('row ' + index + ' out of bounds');
|
||||
_jQuery(matches[0]).find(':visible').each(function() {
|
||||
var element = _jQuery(this);
|
||||
if (angular.isDefined(element.attr('ng:bind'))) {
|
||||
values.push(element.text());
|
||||
}
|
||||
});
|
||||
done(null, values);
|
||||
done(null, matches.bindings());
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -345,8 +345,10 @@ describe("angular.scenario.dsl", function() {
|
|||
beforeEach(function() {
|
||||
doc.append(
|
||||
'<ul>' +
|
||||
' <li ng:repeat-index="0"><span ng:bind="name">misko</span><span ng:bind="gender">male</span></li>' +
|
||||
' <li ng:repeat-index="1"><span ng:bind="name">felisa</span><span ng:bind="gender">female</span></li>' +
|
||||
' <li ng:repeat-index="0"><span ng:bind="name" class="ng-binding">misko</span>' +
|
||||
' <span ng:bind="test && gender" class="ng-binding">male</span></li>' +
|
||||
' <li ng:repeat-index="1"><span ng:bind="name" class="ng-binding">felisa</span>' +
|
||||
' <span ng:bind="gender | uppercase" class="ng-binding">female</span></li>' +
|
||||
'</ul>'
|
||||
);
|
||||
chain = $root.dsl.repeater('ul li');
|
||||
|
|
|
|||
Loading…
Reference in a new issue