Reputation: 101
I want to track changes of attributes of any custom element (or view). So instead of updating each view, or creating super class, I'm trying to use plugin and attach to propertyChanged observer. This way I'm in the context of VM, I have a reference to it, I have a name of changed attribute and new value. I also want a link to a view (Element). So my question is - if I have a VM how do I get a View?
To better understand I also pasting my testing code (my plugin):
let originalCreateObserver = BindableProperty.prototype.createObserver;
BindableProperty.prototype.createObserver = function( viewModel )
{
let changeHandlerName = this.changeHandler;
let name = this.name;
let behaviorPropertyObserver: BehaviorPropertyObserver = originalCreateObserver.apply( this, arguments );
let originalSelfSubscriber = behaviorPropertyObserver['selfSubscriber'];
behaviorPropertyObserver['selfSubscriber'] = function()
{
console.log( 'attr changed', viewModel, name );
// QUESTION: Link to a View?
originalSelfSubscriber.apply( this, arguments );
}
return behaviorPropertyObserver;
}
Bonus question: This is quite monkey-patching. Is there a better way to attach to subscriber?
Upvotes: 0
Views: 167
Reputation: 1921
You can inject Element into your ViewModel which give you the view.
For instance:
@inject(Element)
export class MyClass {
constructor(element) {
this.element = element;
//do something with element;
}
}
Upvotes: 1