Skip to content

Instantly share code, notes, and snippets.

@sukobuto
Created May 23, 2014 03:36
Show Gist options
  • Save sukobuto/3f01ce2b5a84b7f563b5 to your computer and use it in GitHub Desktop.
Save sukobuto/3f01ce2b5a84b7f563b5 to your computer and use it in GitHub Desktop.
【KnockoutJS】View で一時変数を使う ref: http://qiita.com/sukobuto/items/4b5af1a53bf18f81cf7d
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;
<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