From dc949d0fe9b739e1d5dacfbfb2a1c0afb06e4e20 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Wed, 10 Nov 2010 23:26:15 -0800 Subject: [PATCH] Added Image#onload accessors --- src/Image.cc | 35 ++++++++++++++++++++++++++++------- src/Image.h | 2 ++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Image.cc b/src/Image.cc index 62309dd..2178093 100644 --- a/src/Image.cc +++ b/src/Image.cc @@ -22,6 +22,7 @@ Image::Initialize(Handle target) { Local proto = t->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(t, "inspect", Inspect); proto->SetAccessor(String::NewSymbol("src"), GetSrc, SetSrc); + proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload); target->Set(String::NewSymbol("Image"), t->GetFunction()); } @@ -74,14 +75,32 @@ Image::SetSrc(Local, Local val, const AccessorInfo &info) { String::AsciiValue src(val); Image *img = ObjectWrap::Unwrap(info.This()); img->filename = *src; - Handle onload = info.This()->Get(String::New("onload")); - if (onload->IsFunction()) { - img->onload = Persistent::New(Handle::Cast(onload)); - } img->load(); } } +/* + * Get onload callback. + */ + +Handle +Image::GetOnload(Local, const AccessorInfo &info) { + Image *img = ObjectWrap::Unwrap(info.This()); + return img->onload; +} + +/* + * Set onload callback. + */ + +void +Image::SetOnload(Local, Local val, const AccessorInfo &info) { + if (val->IsFunction()) { + Image *img = ObjectWrap::Unwrap(info.This()); + img->onload = Persistent::New(Handle::Cast(val)); + } +} + /* * Initialize a new Image. */ @@ -124,9 +143,11 @@ Image::load() { void Image::loaded() { - // TODO: TryCatch - onload->Call(Context::GetCurrent()->Global(), 0, NULL); - onload.Dispose(); + if (!onload.IsEmpty()) { + // TODO: TryCatch + onload->Call(Context::GetCurrent()->Global(), 0, NULL); + onload.Dispose(); + } Unref(); } diff --git a/src/Image.h b/src/Image.h index 2969f1a..8bafafa 100644 --- a/src/Image.h +++ b/src/Image.h @@ -20,7 +20,9 @@ class Image: public node::ObjectWrap { static Handle New(const Arguments &args); static Handle Inspect(const Arguments &args); static Handle GetSrc(Local prop, const AccessorInfo &info); + static Handle GetOnload(Local prop, const AccessorInfo &info); static void SetSrc(Local prop, Local val, const AccessorInfo &info); + static void SetOnload(Local prop, Local val, const AccessorInfo &info); inline cairo_surface_t *surface(){ return _surface; } cairo_status_t loadSurface(); void loaded();