Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Now supports mediation of attribute values. Great for doing data conv…

…erstion, i.e. dollar as cents to formatted dollars
  • Loading branch information...
commit 214bea6f65a1df3223ac7d47ced8b61be667de8d 1 parent 5467b94
Charlie Morss cmorss authored

Showing 1 changed file with 18 additions and 3 deletions. Show diff stats Hide diff stats

  1. +18 3 lib/appetizer/ui/vendor/js/backbone.modelbinding.js
21 lib/appetizer/ui/vendor/js/backbone.modelbinding.js
@@ -195,12 +195,15 @@ var modelbinding = (function(Backbone, _, $) {
195 195 var elementType = _getElementType(element);
196 196 var attribute_name = config.getBindingValue(element, elementType);
197 197 if (!attribute_name) return;
  198 + var mediator = modelBinding.getMediator(element);
198 199
199   - var modelChange = function(changed_model, val){ element.val(val); };
  200 + var modelChange = function(changed_model, val) {
  201 + element.val(mediator.render(val));
  202 + };
200 203
201 204 var setModelValue = function(attr_name, value){
202 205 var data = {};
203   - data[attr_name] = value;
  206 + data[attr_name] = mediator.parse(value);
204 207 model.set(data);
205 208 };
206 209
@@ -214,7 +217,7 @@ var modelbinding = (function(Backbone, _, $) {
214 217 // set the default value on the form, from the model
215 218 var attr_value = getAttributeValue(model, attribute_name);
216 219 if (typeof attr_value !== "undefined" && attr_value !== null) {
217   - element.val(attr_value);
  220 + element.val(mediator.render(attr_value));
218 221 } else {
219 222 var elVal = element.val();
220 223 if (elVal){
@@ -440,6 +443,7 @@ var modelbinding = (function(Backbone, _, $) {
440 443 var setOnElement = function(element, attr, val){
441 444 var valBefore = val;
442 445 val = modelBinding.Configuration.getDataBindSubst(attr, val);
  446 + val = modelBinding.getMediator(element).render(val);
443 447 switch(attr){
444 448 case "html":
445 449 element.html(val);
@@ -513,6 +517,7 @@ var modelbinding = (function(Backbone, _, $) {
513 517 _.each(databindList, function(databind){
514 518 var eventConfig = getEventConfiguration(element, databind);
515 519 modelBinder.registerDataBinding(model, eventConfig.name, eventConfig.callback);
  520 +
516 521 // set default on data-bind element
517 522 setOnElement(element, databind.elementAttr, getAttributeValue(model, databind.modelAttr));
518 523 });
@@ -544,6 +549,16 @@ var modelbinding = (function(Backbone, _, $) {
544 549 email: {selector: "input[type=email]", handler: StandardBinding}
545 550 };
546 551
  552 + modelBinding.Mediators = {}
  553 +
  554 + modelBinding.getMediator = function(element) {
  555 + var mediator = modelBinding.Mediators[$(element).attr("data-mediator")] || {};
  556 +
  557 + mediator.parse = mediator.parse || function(t) { return t; }
  558 + mediator.render = mediator.render || function(t) { return t; }
  559 + return mediator;
  560 + }
  561 +
547 562 return modelBinding;
548 563 });
549 564

0 comments on commit 214bea6

Please sign in to comment.
Something went wrong with that request. Please try again.