View Single Post
09/05/15, 07:39 AM   #10
Argusus
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 120
Originally Posted by merlight View Post
Should've posted more concrete code -- but it was way too late, sorry -- to show why overriding New isn't needed at all thanks to how it's written (it passes all arguments to Initialize).

Lua Code:
  1. function CustomerList:Initialize(control)
  2.     ZO_SortFilterList.Initialize(self, control)
  3.     self.masterList = {}
  4.     ZO_ScrollList_AddDataType(self.list, 1, "CustomerUnitRow", 30, function(control, data) self:SetupUnitRow(control, data) end)
  5.     ZO_ScrollList_EnableHighlight(self.list, "ZO_ThinListHighlight")
  6.     self.sortFunction = function(listEntry1, listEntry2) return ZO_TableOrderingFunction(listEntry1.data, listEntry2.data, self.currentSortKey, CustomerList.SORT_KEYS, self.currentSortOrder) end
  7.     self.sortHeaderGroup:SelectHeaderByKey("name")
  8.     self:RefreshData()
  9. end
  10.  
  11. TRADESMAN.CustomerList = CustomerList:New(CustomerListContainer)

This way CustomerListContainer is not hard-coded in the class, thus you could create multiple instances (on different container controls). Not really useful in your case, I know, but still cleaner IMO.

Another little suggestion that's good getting used to. In BuildMasterList(), don't create a new table, discarding the old one -- unless you have a million items in there and really want to free the memory and only insert a thousand -- just clear the existing table with
Lua Code:
  1. ZO_ClearNumericallyIndexedTable(self.masterList)
I really like this suggestion. I have been wanting to clean up tradesman for a while now. I think making a reusable constructor would be a step in the right direction. Below is a shorter version of my "BuildMasterList"

Code:
function CustomerList:New()
	local customers = ZO_SortFilterList.New(self, CustomerListContainer)
	
	customers.masterList = {}
	
 	ZO_ScrollList_AddDataType(customers.list, 1, "CustomerUnitRow", 30, function(control, data) customers:SetupUnitRow(control, data) end)
 	ZO_ScrollList_EnableHighlight(customers.list, "ZO_ThinListHighlight")	
 	customers.sortFunction = function(listEntry1, listEntry2) return ZO_TableOrderingFunction(listEntry1.data, listEntry2.data, customers.currentSortKey, CustomerList.SORT_KEYS, customers.currentSortOrder) end
	customers.sortHeaderGroup:SelectHeaderByKey("name")
	customers:RefreshData()
		
	return customers
end

function CustomerList:BuildMasterList()
	self.masterList = {}	
    	local transactions = TRADESMAN.savedVariables.Transactions
	table.insert(self.masterList, transactions)
    end		
end
Have you looked at the code for tradesman? I have 4-5 of these scroll lists, and basically the same code to create each except for the container control and the sort keys that are needed to be unique, then after that when buildmasterlist, I access the saved variables to populate the scroll lists. I wonder how I could have all of my scroll lists share the same initialization code but have unique sort keys, and build master list, passing in unique containers you explained well.

Last edited by Argusus : 09/05/15 at 08:23 AM.
  Reply With Quote