The way Lua handles
obj.method() vs
obj:method() is simple, but not straightforward.
When calling a function,
obj:method(...) is the same as
obj.method(obj,...).
When defining a function,
obj:method(...) is the same as
obj.method(self,...).
Lua will handle mixing these syntaxes as long as you take the above rule in mind.
If you call
obj.method(frame), a function defined as
obj:method() will receive
frame and put it in
self.
One way or another, you need a reference to your frame to work on it. The WoW API doesn't set any globals when a frame script runs.
The way I would implement your code with this in mind and make room for easier expansion:
Lua Code:
-- __mode defines whether (k)eys and/or (v)alues are released to the GC if this table is the only remaining reference to them (in either case, the entire table entry is removed)
-- Frames can't be garbage collected, but it's still good practice for memory management if you're storing external objects
local OriginalMeta=setmetatable({},{__mode="kv"});
-- ListView frame metatable set to refer to its original metatable as a fallback (Stored by LibListView:New())
local ListViewMeta={__index=setmetatable({},{__index=function(obj,key) return OriginalMeta[obj][key]; end})};
LibListView={};
function LibListView:New()-- self is our new frame (call as LibListView.New(frame))
assert(OriginalMeta[self]==nil,"Already attached to frame");
OriginalMeta[self]=getmetatable(self);-- Store original metatable as a fallback
setmetatable(self,ListViewMeta);-- Set new metatable
self:Initialize();
end
-- Frame methods injected by our library
function ListViewMeta.__index:Initialize()
-- Initialize instanced data and store to frame
self.Buttons={};
self.Data={};
end
function ListViewMeta.__index:SetWidgetScript(something)
print(something);
end
OnLoad Handler:
Code:
LibListView.New(self)
Test Script:
Lua Code:
local listview = CreateFrame("Frame","listview",UIParent,"TemplateListViewTemplate")
listview:SetSize(150, 100)
listview:SetPoint("TOPLEFT",50,-75)
listview:SetWidgetScript("Hello World")
One major change was to separate your static functions and instanced data. The frame methods were moved to their own metatable and are injected into the frame. As the frame is basically a table itself, data values are written directly into it.