Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions dist/transparency.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ helpers = require('./helpers');

Context = require('./context');

Transparency = {};
Transparency = {
globalDirectives: {}
};

Transparency.render = function(context, models, directives, options) {
var base, log;
Expand All @@ -33,6 +35,10 @@ Transparency.render = function(context, models, directives, options) {
return context.render(models, directives, options).el;
};

Transparency.setGlobalDirectives = function(directives) {
return this.globalDirectives = directives;
};

Transparency.matcher = function(element, key) {
return element.el.id === key || indexOf.call(element.classNames, key) >= 0 || element.el.name === key || element.el.getAttribute('data-bind') === key;
};
Expand Down Expand Up @@ -672,7 +678,9 @@ module.exports = Instance = (function() {
});

Instance.prototype.renderDirectives = chainable(function(model, index, directives) {
var attributes, element, key, results;
var attributes, element, key, results, temp;
temp = _.extend({}, this.Transparency.globalDirectives);
directives = _.extend(temp, directives);
results = [];
for (key in directives) {
if (!hasProp.call(directives, key)) continue;
Expand Down Expand Up @@ -753,6 +761,22 @@ _.toArray = function(obj) {
return arr;
};

_.extend = function(destination, source) {
if (typeof destination === 'undefined') {
destination = {};
}
for (var property in source) {
if (source[property] && source[property].constructor &&
source[property].constructor === Object) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
};

_.isString = function(obj) { return _.toString.call(obj) == '[object String]'; };

_.isNumber = function(obj) { return _.toString.call(obj) == '[object Number]'; };
Expand Down
2 changes: 1 addition & 1 deletion dist/transparency.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ module.exports = Instance = (function() {
});

Instance.prototype.renderDirectives = chainable(function(model, index, directives) {
var attributes, element, key, results;
var attributes, element, key, results, temp;
temp = _.extend({}, this.Transparency.globalDirectives);
directives = _.extend(temp, directives);
results = [];
for (key in directives) {
if (!hasProp.call(directives, key)) continue;
Expand Down
16 changes: 16 additions & 0 deletions lib/lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ _.toArray = function(obj) {
return arr;
};

_.extend = function(destination, source) {
if (typeof destination === 'undefined') {
destination = {};
}
for (var property in source) {
if (source[property] && source[property].constructor &&
source[property].constructor === Object) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
};

_.isString = function(obj) { return _.toString.call(obj) == '[object String]'; };

_.isNumber = function(obj) { return _.toString.call(obj) == '[object Number]'; };
Expand Down
8 changes: 7 additions & 1 deletion lib/transparency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ helpers = require('./helpers');

Context = require('./context');

Transparency = {};
Transparency = {
globalDirectives: {}
};

Transparency.render = function(context, models, directives, options) {
var base, log;
Expand All @@ -32,6 +34,10 @@ Transparency.render = function(context, models, directives, options) {
return context.render(models, directives, options).el;
};

Transparency.setGlobalDirectives = function(directives) {
return this.globalDirectives = directives;
};

Transparency.matcher = function(element, key) {
return element.el.id === key || indexOf.call(element.classNames, key) >= 0 || element.el.name === key || element.el.getAttribute('data-bind') === key;
};
Expand Down
1 change: 1 addition & 0 deletions spec/functionalSpecRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<script src="listsSpec.js"></script>
<script src="modelReferencesSpec.js"></script>
<script src="nestedModelsSpec.js"></script>
<script src="globalDirectives.js"></script>

<script>
mocha.checkLeaks();
Expand Down
67 changes: 67 additions & 0 deletions spec/globalDirectives.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
describe "Transparency", ->

it "should render plain values with global directives", ->


globalDirective =
'global-test':
title : (params) ->
return this.name
'global-backgroundColor':
style : (params) ->
return 'background-color: ' + this.color


template = $ """
<div>
<div data-bind="loop">
<span data-bind="name" class="global-test"></span>
<div data-bind="cars">
<span data-bind="color" class="global-backgroundColor"></span>
</div>
<hr>
</div>
</div>
"""

data =
loop: [
{
"name" : "John"
"cars" : [
{"color" : "red"}
{"color" : "blue"}
]
}
{
"name" : "Michael"
"cars" : [
{"color" : "yellow"}
{"color" : "green"}
]
}
]

expected = $ """
<div>
<div data-bind="loop">
<span data-bind="name" class="global-test" title="John">John</span>
<div data-bind="cars">
<span data-bind="color" class="global-backgroundColor" style="background-color: red">red</span>
<span data-bind="color" class="global-backgroundColor" style="background-color: blue">blue</span>
</div>
<hr>
<span data-bind="name" class="global-test" title="Michael">Michael</span>
<div data-bind="cars">
<span data-bind="color" class="global-backgroundColor" style="background-color: yellow">yellow</span>
<span data-bind="color" class="global-backgroundColor" style="background-color: green">green</span>
</div>
<hr>
</div>
</div>
"""

Transparency.setGlobalDirectives globalDirective

template.render data
expect(template).toBeEqual expected
48 changes: 48 additions & 0 deletions spec/globalDirectives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
(function() {
describe("Transparency", function() {
return it("should render plain values with global directives", function() {
var data, expected, globalDirective, template;
globalDirective = {
'global-test': {
title: function(params) {
return this.name;
}
},
'global-backgroundColor': {
style: function(params) {
return 'background-color: ' + this.color;
}
}
};
template = $("<div>\n <div data-bind=\"loop\">\n <span data-bind=\"name\" class=\"global-test\"></span>\n <div data-bind=\"cars\">\n <span data-bind=\"color\" class=\"global-backgroundColor\"></span>\n </div>\n <hr>\n </div>\n</div>");
data = {
loop: [
{
"name": "John",
"cars": [
{
"color": "red"
}, {
"color": "blue"
}
]
}, {
"name": "Michael",
"cars": [
{
"color": "yellow"
}, {
"color": "green"
}
]
}
]
};
expected = $("<div>\n <div data-bind=\"loop\">\n <span data-bind=\"name\" class=\"global-test\" title=\"John\">John</span>\n <div data-bind=\"cars\">\n <span data-bind=\"color\" class=\"global-backgroundColor\" style=\"background-color: red\">red</span>\n <span data-bind=\"color\" class=\"global-backgroundColor\" style=\"background-color: blue\">blue</span>\n </div>\n <hr>\n <span data-bind=\"name\" class=\"global-test\" title=\"Michael\">Michael</span>\n <div data-bind=\"cars\">\n <span data-bind=\"color\" class=\"global-backgroundColor\" style=\"background-color: yellow\">yellow</span>\n <span data-bind=\"color\" class=\"global-backgroundColor\" style=\"background-color: green\">green</span>\n </div>\n <hr>\n </div>\n</div>");
Transparency.setGlobalDirectives(globalDirective);
template.render(data);
return expect(template).toBeEqual(expected);
});
});

}).call(this);
4 changes: 3 additions & 1 deletion src/instance.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ module.exports = class Instance
#
# Directives are executed after the default rendering, so that they can be used for overriding default rendering.
renderDirectives: chainable (model, index, directives) ->
temp = _.extend({}, @Transparency.globalDirectives)
directives = _.extend(temp, directives)

for own key, attributes of directives when typeof attributes == 'object'
model = {value: model} unless typeof model == 'object'

Expand All @@ -153,4 +156,3 @@ module.exports = class Instance
elements = @queryCache[key] ||= (el for el in @elements when @Transparency.matcher el, key)
helpers.log "Matching elements for '#{key}':", elements
elements

7 changes: 6 additions & 1 deletion src/transparency.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Context = require './context'
# For the full API reference, please see the README.

# ## Public API
Transparency = {}
Transparency = {
globalDirectives : {}
}

# `Transparency.render` maps JSON objects to DOM elements.
Transparency.render = (context, models = [], directives = {}, options = {}) ->
Expand All @@ -50,6 +52,9 @@ Transparency.render = (context, models = [], directives = {}, options = {}) ->
# reflow calculations are not triggered. So, detach it before rendering.
context.render(models, directives, options).el

Transparency.setGlobalDirectives = (directives) ->
@globalDirectives = directives

# ### Configuration

# By default, Transparency matches model properties to elements by `id`, `class`, `name` and `data-bind` attributes.
Expand Down