d0ec373e by Zack Owens

allowing formatters to be passed in as options

1 parent 5cd20248
describe('Rivets.Binding', function() {
var model, el, view, binding;
var model, el, view, binding, opts;
beforeEach(function() {
rivets.configure({
......@@ -13,7 +13,8 @@ describe('Rivets.Binding', function() {
el = document.createElement('div');
el.setAttribute('data-text', 'obj.name');
view = rivets.bind(el, {obj: {}});
opts = {};
view = rivets.bind(el, {obj: {}}, opts);
binding = view.bindings[0];
model = binding.model;
});
......@@ -310,6 +311,12 @@ describe('Rivets.Binding', function() {
expect(binding.formattedValue('hat')).toBe('model awesome hat');
});
it('uses formatters from the bind options', function() {
opts.formatters = { optAwesome: function(value) { return 'option awesome ' + value; } };
binding.formatters.push("optAwesome");
expect(binding.formattedValue('hat')).toBe('option awesome hat');
});
describe('with a multi-argument formatter string', function() {
beforeEach(function() {
rivets.formatters.awesome = function(value, prefix) {
......
......@@ -40,6 +40,8 @@ class Rivets.Binding
formatter = if @model[id] instanceof Function
@model[id]
else if @options?.bindingOptions?.formatters?[id] instanceof Function
@options.bindingOptions.formatters[id]
else
Rivets.formatters[id]
......@@ -127,7 +129,7 @@ class Rivets.Binding
class Rivets.View
# The DOM elements and the model objects for binding are passed into the
# constructor.
constructor: (@els, @models) ->
constructor: (@els, @models, @options) ->
@els = [@els] unless (@els.jquery || @els instanceof Array)
@build()
......@@ -166,6 +168,9 @@ class Rivets.View
if bindingRegExp.test attribute.name
options = {}
if @options? and typeof @options is 'object'
options.bindingOptions = @options
type = attribute.name.replace bindingRegExp, ''
pipes = (pipe.trim() for pipe in attribute.value.split '|')
context = (ctx.trim() for ctx in pipes.shift().split '<')
......@@ -394,8 +399,8 @@ rivets =
# Binds a set of model objects to a parent DOM element. Returns a Rivets.View
# instance.
bind: (el, models = {}) ->
view = new Rivets.View(el, models)
bind: (el, models = {}, options) ->
view = new Rivets.View(el, models, options)
view.bind()
view
......