Skip to content

Commit

Permalink
Added $element pseudo-variable that allows accessing the current elem…
Browse files Browse the repository at this point in the history
…ent (and through it the DOM tree).
  • Loading branch information
studgeek committed Oct 3, 2011
1 parent 0cbcbad commit 271d5b8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
11 changes: 6 additions & 5 deletions build/output/knockout-latest.debug.js
Expand Up @@ -2,7 +2,7 @@
// (c) Steven Sanderson - http://knockoutjs.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)

(function(window,undefined){
(function(window,undefined){
var ko = window["ko"] = {};
// Google Closure Compiler helpers (used only to make the minified file smaller)
ko.exportSymbol = function(publicPath, object) {
Expand Down Expand Up @@ -1582,7 +1582,7 @@ ko.exportSymbol('ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson', ko

'getBindings': function(node, bindingContext) {
var bindingsString = this['getBindingsString'](node, bindingContext);
return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext) : null;
return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext, node) : null;
},

// The following function is only used internally by this default provider.
Expand All @@ -1597,11 +1597,12 @@ ko.exportSymbol('ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson', ko

// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
'parseBindingsString': function(bindingsString, bindingContext) {
'parseBindingsString': function(bindingsString, bindingContext, node) {
try {
var viewModel = bindingContext['$data'];
var rewrittenBindings = " { " + ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(bindingsString) + " } ";
return ko.utils.evalWithinScope(rewrittenBindings, viewModel === null ? window : viewModel, bindingContext);
return ko.utils.evalWithinScope(rewrittenBindings, viewModel === null ? window : viewModel,
bindingContext, { "$element" : node });
} catch (ex) {
throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString);
}
Expand Down Expand Up @@ -3051,4 +3052,4 @@ ko.exportSymbol('ko.nativeTemplateEngine', ko.nativeTemplateEngine);(function()
ko.setTemplateEngine(jqueryTmplTemplateEngineInstance);

ko.exportSymbol('ko.jqueryTmplTemplateEngine', ko.jqueryTmplTemplateEngine);
})();})(window);
})();})(window);
8 changes: 7 additions & 1 deletion spec/bindingAttributeBehaviors.js
Expand Up @@ -159,7 +159,13 @@ describe('Binding attribute syntax', {
value_of(passedValues[1]).should_be("goodbye");
},

'Should be able to refer to the bound object itself (at the root scope, the viewmodel) via $data': function() {
'Should be able to use $element in binding value': function() {
testNode.innerHTML = "<div data-bind='text: $element.tagName'></div>";
ko.applyBindings({}, testNode);
value_of(testNode).should_contain_text("DIV");
},

'Should be able to refer to the bound object itself (at the root scope, the viewmodel) via $data': function() {
testNode.innerHTML = "<div data-bind='text: $data.someProp'></div>";
ko.applyBindings({ someProp: 'My prop value' }, testNode);
value_of(testNode).should_contain_text("My prop value");
Expand Down
7 changes: 4 additions & 3 deletions src/binding/bindingProvider.js
Expand Up @@ -14,7 +14,7 @@

'getBindings': function(node, bindingContext) {
var bindingsString = this['getBindingsString'](node, bindingContext);
return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext) : null;
return bindingsString ? this['parseBindingsString'](bindingsString, bindingContext, node) : null;
},

// The following function is only used internally by this default provider.
Expand All @@ -29,11 +29,12 @@

// The following function is only used internally by this default provider.
// It's not part of the interface definition for a general binding provider.
'parseBindingsString': function(bindingsString, bindingContext) {
'parseBindingsString': function(bindingsString, bindingContext, node) {
try {
var viewModel = bindingContext['$data'];
var rewrittenBindings = " { " + ko.jsonExpressionRewriting.insertPropertyAccessorsIntoJson(bindingsString) + " } ";
return ko.utils.evalWithinScope(rewrittenBindings, viewModel === null ? window : viewModel, bindingContext);
return ko.utils.evalWithinScope(rewrittenBindings, viewModel === null ? window : viewModel,
bindingContext, { "$element" : node });
} catch (ex) {
throw new Error("Unable to parse bindings.\nMessage: " + ex + ";\nBindings value: " + bindingsString);
}
Expand Down

0 comments on commit 271d5b8

Please sign in to comment.