Re: WebView Bindings
Re: WebView Bindings
- Subject: Re: WebView Bindings
- From: Sarat Kongara <email@hidden>
- Date: Thu, 23 Jun 2005 14:00:29 -0700
Hi Guys,
I guess implementing a custom WebView is the only option for this
binding. I haven't received any response for my earlier post. I have
implemented a custom WebView to expose a "source" binding. I am not
sure how to handle the propogation of editing changes in the WebView
(editable) to the model. I know that I need to use KVC but I am not
sure which method I need to overwrite in the WebView, which method
gets called when the user starts typing text in the WebView.
Below is the code I have so far.
The code in the method - (void)updateForMouseEvent:(NSEvent *)event
should be moved to the correct method.
Any help is appreciated. Thanks.
#import <Cocoa/Cocoa.h>
#import <WebView/WebView.h>
@interface LSWebView : WebView {
id observedObjectForSource;
NSString *observedKeyPathForSource;
}
@end
#import "LSWebView.h"
static void *SourceBindingIdentifier = (void *)@"WebViewSource";
@implementation LSWebView
- (void)dealloc {
[self unbind:@"source"];
[super dealloc];
}
- (NSArray *)exposedBindings {
NSMutableArray *bindingNames = [NSMutableArray arrayWithArray:[super
exposedBindings]];
[bindingNames addObject:@"source"];
return bindingNames;
}
- (Class)valueClassForBinding:(NSString *)binding {
if([binding isEqualToString:@"source"])
return [NSString class];
else
return [super valueClassForBinding:binding];
}
- (void)bind:(NSString *)binding
toObject:(id)observableObject
withKeyPath:(NSString *)keyPath
options:(NSDictionary *)options {
// Observe the observableObject for changes -- note, pass binding identifier
// as the context, so you get that back in observeValueForKeyPath:...
// This way you can easily determine what needs to be updated.
if([binding isEqualToString:@"source"]) {
[observableObject addObserver:self
forKeyPath:keyPath
options:0
context:SourceBindingIdentifier];
// Register what object and what keypath are associated with this binding
observedObjectForSource = [observableObject retain];
observedKeyPathForSource = [keyPath copy];
} else {
[super bind:binding toObject:observableObject withKeyPath:keyPath
options:options];
}
}
- (NSDictionary *)infoForBinding:(NSString *)binding {
if([binding isEqualToString:@"source"]) {
[NSDictionary
dictionaryWithObjectsAndKeys:observedObjectForSource,NSObservedObjectKey,
observedKeyPathForSource, NSObservedKeyPath, nil];
} else {
return [super infoForBinding:binding];
}
}
- (void)unbind:(NSString *)binding {
if([binding isEqualToString:@"source"]) {
[observedObjectForSource release];
[observedKeyPathForSource release];
} else {
[super unbind:binding];
}
}
- (void)updateForMouseEvent:(NSEvent *)event {
if (observedObjectForSource != nil) {
NSString *newSource = [(DOMHTMLElement *)[[[self mainFrame]
DOMDocument] documentElement] outerHTML];
[observedObjectForSource setValue:newSource
forKeyPath:observedKeyPathForSource];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
// You passed the binding identifier as the context when registering
// as an observer--use that to decide what to update...
if (context == SourceBindingIdentifier) {
NSString *newSource = [observedObjectForSource
valueForKeyPath:observedKeyPathForSource];
[[self mainFrame] loadHTMLString:newSource baseURL:nil];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change
context:context];
}
}
@end
Regards
Sarat
On 6/22/05, Sarat Kongara <email@hidden> wrote:
> Hi Guys,
> Is there a simple way to bind to the html content displayed by
> WebView. I don't see a Content or HTMLContent binding for the WebView.
> If there is none, one way to do this is to create a subclass of
> WebView that exposes this (HTMLContent) binding right? Just want to
> know if I am on the right track and not duplicating something that's
> already there. Thanks.
>
> Regards
> Sarat
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden