Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link property back to the entity and property description #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

andrew-veresov
Copy link

This is to allow creating bindings which bound to a property and use entity to which property belongs to and/or property metadata internally.

For example next binding adds isChanged computed to the property and use created computed observable to add/remove 'changed' class to the element:

(function (ko) {

    ko.bindingHandlers['breeze'] = {
        'after': ['value', 'attr'],
        'init': function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var options = valueAccessor();
            if (options != undefined) {
                if (options.trackChanges != undefined && ko.isObservable(options.trackChanges) && options.trackChanges.parentEntity != undefined) {
                    var entity = options.trackChanges.parentEntity;
                    var propName = options.trackChanges.propertyDescriptor.name;

                    options.trackChanges.isChanged = ko.computed(function () {
                        return entity.entityAspect.originalValues.hasOwnProperty(propName)
                            & entity.entityAspect.originalValues[propName] != options.trackChanges();
                    });

                    options.trackChanges.isChanged.subscribe(function (isChanged) {
                        if (isChanged) {
                            $(element).addClass('changed');
                        } else {
                            $(element).removeClass('changed');
                        }
                    })
                }
                //other bindings...
                if (options.validate != undefined && ko.isObservable(options.validate) && options.validate.parentEntity != undefined) {
                    var propertyName = options.validate.propertyDescriptor.name;
                    ko.bindingHandlers.breezevalidation.init(element, function() { return { properties: [propertyName] }; }, allBindings, viewModel, bindingContext);
                }
            }
        }
    };

})(window.ko || require(['ko']));

Using above binding we can enable per property change tracking only for specific properties which need it and avoid spawning isChanged computed for all properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant