client/components/ianstormtaylor/reactive/0.13.2/lib/text-binding.js
/**
* Module dependencies.
*/
var debug = require('debug')('reactive:text-binding');
var utils = require('./utils');
/**
* Expose `TextBinding`.
*/
module.exports = TextBinding;
/**
* Initialize a new text binding.
*
* @param {Reactive} view
* @param {Element} node
* @param {Attribute} attr
* @api private
*/
function TextBinding(reactive, node) {
this.reactive = reactive;
this.text = node.data;
this.node = node;
this.props = utils.interpolationProps(this.text);
this.subscribe();
this.render();
}
/**
* Subscribe to changes.
*/
TextBinding.prototype.subscribe = function(){
var self = this;
var reactive = this.reactive;
this.props.forEach(function(prop){
reactive.sub(prop, function(){
self.render();
});
});
};
/**
* Render text.
*/
TextBinding.prototype.render = function(){
var node = this.node;
var text = this.text;
var reactive = this.reactive;
var model = reactive.model;
// TODO: delegate most of this to `Reactive`
debug('render "%s"', text);
node.data = utils.interpolate(text, function(prop, fn){
if (fn) {
return fn(model, reactive.view, utils.call);
} else {
return reactive.get(model, prop);
}
});
};