开发者

Binding to view model properties instead of global variables

开发者 https://www.devze.com 2023-04-05 05:40 出处:网络
If I have the following code, where knockout binds the textarea to Text the globa开发者_StackOverflow中文版l object, instead of Text on the viewModel. Can knockout be told to use any local property of

If I have the following code, where knockout binds the textarea to Text the globa开发者_StackOverflow中文版l object, instead of Text on the viewModel. Can knockout be told to use any local property of the viewModel before trying global ones?

I know I can do viewModel.Text, but I prefer not to. Besides, I might want to use the same code for myotherviewModel without changing the markup.

I'd prefer to do window.Text if I want to call the global function.

<textarea data-bind="value:Text"></textarea>

JS:

var viewModel={
    Text : 'my text'    
};

ko.applyBindings(viewModel)


Can you clarify your scenario? KO does a with when evaluating bindings, so the viewModel variables would be found before a global variable like in this sample: http://jsfiddle.net/rniemeyer/Um6Y6/ – RP Niemeyer Sep 15 at 12:49

0

精彩评论

暂无评论...
验证码 换一张
取 消