-
-
Save sukobuto/3f01ce2b5a84b7f563b5 to your computer and use it in GitHub Desktop.
【KnockoutJS】View で一時変数を使う ref: http://qiita.com/sukobuto/items/4b5af1a53bf18f81cf7d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.bindingHandlers['variable'] = { | |
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { | |
var variables = valueAccessor(); | |
for ( var v in variables ) { | |
if (variables.hasOwnProperty(v)) { | |
if (ko.isObservable(variables[v])) { | |
bindingContext[v] = variables[v]; | |
} else if (typeof variables[v] === 'function') { | |
if (ko.hasOwnProperty('defineProperty')) | |
ko.defineProperty(bindingContext, v, variables[v]); | |
else bindingContext[v] = ko.computed(variables[v]); | |
} else { | |
bindingContext[v] = variables[v]; | |
} | |
} | |
} | |
} | |
}; | |
ko.virtualElements.allowedBindings['variable'] = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div data-bind="variable: { foo: function(){ return doSomething(someProperty) } }"> | |
<!--ko if: foo == 'hoge'--> | |
<span data-bind="text: foo"></span> | |
<!--/ko--> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment