Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added tests for options binding; fixed a bug where options bound afte…

…r values caused the wrong value to be selected
  • Loading branch information...
commit 3f8018b3b5f543e97e72b898568c727da028cad6 1 parent 6ed8081
politician authored
View
15 outback.js
@@ -281,7 +281,7 @@
}
function applyBinding (view, binding) {
- var binders, binderArgs, eventName, updateFn;
+ var binders, binderArgs, eventName, updateFn, fn;
binders = {
modelSubs: [],
@@ -299,10 +299,12 @@
updateFn = binding.handler.update;
- binders.updates.push(function() {
+ binders.updates.push(fn = function() {
updateFn.apply(view, binderArgs);
});
+ fn.priority = +binding.handler.updatePriority || 10;
+
binders.modelSubs.push(function() {
binding.modelEvents.subscribe(binding.modelEvents.eventName, function(m, val) {
updateFn.apply(view, binderArgs);
@@ -323,7 +325,7 @@
binders.removes.push(function() {
binding.handler.remove.apply(view, binderArgs);
});
- }
+ }
return binders;
}
@@ -409,7 +411,7 @@
// Run binders in stages because we want to avoid cascades.
eachfn(allBinders.inits);
- eachfn(allBinders.updates);
+ eachfn(_.sortBy(allBinders.updates, function (fn) { return fn.priority; }));
eachfn(allBinders.modelSubs);
if (typeof view.bindingSummary === 'function') {
@@ -1178,8 +1180,9 @@
} else {
$(element).html(config.noContent);
}
- }
+ },
+ updatePriority: 1
};
- })();
+ }());
}));
View
1  spec/SpecRunner.html
@@ -62,6 +62,7 @@
<script type="text/javascript" src="bindings/hasfocus.spec.js"></script>
<script type="text/javascript" src="bindings/checked.spec.js"></script>
<script type="text/javascript" src="bindings/currency.spec.js"></script>
+ <script type="text/javascript" src="bindings/options.spec.js"></script>
<!---->
</head>
<body>
View
42 spec/bindings/options.spec.js
@@ -0,0 +1,42 @@
+describe('the options binding', function() {
+
+ describe('should use a selector to find option and optgroup elements for a select control', function() {
+
+ beforeEach(function() {
+ this.model = new AModel({transport: 'boat'});
+ this.viewModel = new AModel({transportOptions: '#transportOptions'});
+ this.view = new FixtureView({model: this.model});
+ _.extend(this.view, {
+ viewModel: this.viewModel,
+ innerHtml:
+ "<select data-bind='value: @transport' data-bind-view='options: @transportOptions'></select>\
+ <script type='text/template' id='transportOptions'>\
+ <option value='car'>Car</option>\
+ <option value='boat'>Boat</option>\
+ <option value='train'>Train</option>\
+ </script>"
+ });
+
+ this.view.render();
+ this.el = this.view.$('#anchor select');
+ });
+
+ afterEach(function() {
+ this.view.remove();
+ });
+
+ it('should add options to the select control', function() {
+ expect(this.el.find('option').size()).toBe(3);
+ });
+
+ it('should set the correct initial value', function() {
+ expect(this.el.val()).toBe('boat');
+
+ var boat = this.el.find('option[value=boat]');
+
+ expect(boat.size() > 0).toBeTruthy();
+ expect(boat.is(':selected')).toBeTruthy();
+ });
+ });
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.