Forum > Beginners

FPOObservedChanged

(1/2) > >>

Weitentaaal:
Hello Guys,

Attached a small Project, in which i played a bit with Observers. i  understand what Observers do but i cant get how you handle this certain Function (FPOObservedChanged).

Could you guys tell me what i am doing wrong and what i should do instead ? I got another Project in which i wanted to implement Observers. Its Huge so i wanna be careful with those Observers.

For Example if i have 3 edits and all do notify my ViewModel via this Procedure. How do i know where i get my Data from ? was it send by Edit1 or Edit2 ?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMyObserver.FPOObservedChanged(ASender: TObject;   Operation: TFPObservedOperation; Data: Pointer);var  intf: IFPObserved;begin  case Operation of    ooFree: if Supports(ASender, IFPObserved, intf) then              intf.FPODetachObserver(Self);    ooCustom: begin       If Assigned(Data) then begin          case ASender.ClassName of //i know this wont work. what do i do here ?             'Edit1': SomStr :=  String(Data);             'Edit2': Num1 := Integer(Data);             'Edit3': Num1 := Integer(Data);             'ComboBox1': begin                Case Integer(Data) of                   0: fNewColor:= clBlue;                   1: fNewColor:= clGreen;                   2: fNewColor:= clYellow;                   3: fNewColor:= clRed;                   4: fNewColor:= clWhite;                   5: fNewColor:= clBlack;                   6: fNewColor:= clAqua;                end;             end;          end;       end;    end;  end;end; 
and if i have more than 1 Components of the Same type, lets say TEdit, can i create an Observer which observes all of those Edit's ? then again how do i know from who i got the data from ?

Thanks in advance :)

AlexTP:
Perhaps, you may check the ASender and compare it with Edit1, Edit2. Also you may test 'if ASender is TEdit'.

Weitentaaal:

--- Quote from: Alextp on December 02, 2021, 11:30:58 am ---Perhaps, you may check the ASender and compare it with Edit1, Edit2. Also you may test 'if ASender is TEdit'.

--- End quote ---

Thanks for your Reply. i was about to decouple my GUI from my Logic (to improve legibility and Maintenance). If there is no other way, then i will go with that, but after me, the Code will get a bit messy when using it like this.

Edit: If the code i have Shown is pointless(because its not done like this or whatever) then please tell me :)

AlexTP:
You can also assign Tag property of TEdit's, then in the observer:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if ASender is TComponent then begin  NTag:= TComponent(ASender).Tag;  ...end

Weitentaaal:

--- Quote from: Alextp on December 02, 2021, 12:59:24 pm ---You can also assign Tag property of TEdit's, then in the observer:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if ASender is TComponent then begin  NTag:= TComponent(ASender).Tag;  ...end
--- End quote ---

was thinking about this but some of my Components (TListBoxes) do already have the Tag Property Occupied. So i would have to create maybe a record to store in them and then it is getting complex again. But this is probably the best way to do this i guess.

Thank you @Alextp

Keep me updated if any1 has another Solution.

Edit: Question: isn't it resource-intensive if i update All my Controls after every Change ? Should i Split the Update Procedure up on multiple Procedures to Update certain parts of my View ?

Navigation

[0] Message Index

[#] Next page

Go to full version