Ko Knockoutjs dynamic Attribute binding techniques using _javascript techniques

Source: Internet
Author: User
Knockoutjs abbreviation KO
Ko's dynamic properties refer to ViewModel properties that are not determined, and those that are later required.
What is an indeterminate attribute, such as Listmodel if you edit an item, you want to change the status of this item to edit. The data does not include the edit attribute, and when MVVM is tied, an error is found.
Then it is necessary to expand KO to achieve our goal.
The first is to recognize the value attribute binding, and the value-free attribute binding:
one, a value attribute binding
JS Model:
Copy Code code as follows:

$ (function () {
var ViewModel = function () {
var self = this;
Self.text = ko.observable (1);
};
Ko.applybindings (New ViewModel ());
});

UI bindings:
Copy Code code as follows:

<div data-bind= ' Text:text ' ></div>

Present:

non-value attribute binding
JS Model:
Copy Code code as follows:

$ (function () {
var ViewModel = function () {
var self = this;
Self.text = Ko.observable ();
};
Ko.applybindings (New ViewModel ());
});

Of course text is a generic value type, as is the case with a value attribute binding, if the value type is an object, and a property is used with:
UI bindings:
Copy Code code as follows:

<div data-bind= ' With:text ' >
<div data-bind= "Text:property" ></div>
</div>

Three, dynamic property binding:
Dynamic property binding, then the property itself is not sure, the method of using Ko is difficult to implement, so need to expand.
JS Expansion:
Copy Code code as follows:

Smoke and mirrors q:397386036
Ko.bindingHandlers.ext = {
Update:function (element, Valueaccessor, Allbindingsaccessor, ViewModel, BindingContext) {
var value = ko.utils.unwrapObservable (Valueaccessor ());
For (var handler in value) {
if (Value.hasownproperty (handler)) {
if (typeof Viewmodel[value[handler]] = = ' undefined ') {
Viewmodel[value[handler]] = ko.observable ();
}
Ko.bindinghandlers[handler].update (element, function () {return viewmodel[value[handler]];
}
}
}
};

JS Model:
Copy Code code as follows:

$ (function () {
var ViewModel = function () {
};
Ko.applybindings (New ViewModel ());
});

UI bindings:
Copy Code code as follows:

<div data-bind= "ext:{text: ' Text '}" ></div>
<!--events for easy testing-->
<a href= "javascript:void (0)" data-bind= "Click:function () {$data. Text (1);}" > Change text value </a>

EXT, the first text is the Ko text method, the second text must be a string, is the Context/viewmodel property. So the dynamic properties of ext can be used in any Ko method, such as Ext:{text: ' Text ', Value: ' Text '}
Present:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.