Skip to content

Commit

Permalink
refactored observable
Browse files Browse the repository at this point in the history
  • Loading branch information
sglienke committed Jun 7, 2015
1 parent 5b86e99 commit a12ecb1
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 185 deletions.
34 changes: 25 additions & 9 deletions MainView.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ object MainViewForm: TMainViewForm
Left = 0
Top = 0
Caption = 'SimpleMVVMDemo'
ClientHeight = 289
ClientWidth = 561
ClientHeight = 373
ClientWidth = 601
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Expand All @@ -17,8 +17,8 @@ object MainViewForm: TMainViewForm
object GroupBox1: TGroupBox
Left = 8
Top = 8
Width = 161
Height = 121
Width = 185
Height = 153
Caption = 'Example 1'
TabOrder = 0
object lblFullName: TLabel
Expand Down Expand Up @@ -46,10 +46,10 @@ object MainViewForm: TMainViewForm
end
end
object GroupBox2: TGroupBox
Left = 184
Left = 208
Top = 8
Width = 169
Height = 169
Width = 185
Height = 153
Caption = 'Example 2'
TabOrder = 1
object lblClickCount: TLabel
Expand Down Expand Up @@ -84,10 +84,10 @@ object MainViewForm: TMainViewForm
end
end
object GroupBox3: TGroupBox
Left = 368
Left = 408
Top = 8
Width = 185
Height = 169
Height = 153
Caption = 'Example 3'
TabOrder = 2
object lblPrice: TLabel
Expand All @@ -114,4 +114,20 @@ object MainViewForm: TMainViewForm
TabOrder = 1
end
end
object GroupBox4: TGroupBox
Left = 8
Top = 176
Width = 185
Height = 161
Caption = 'Example 4'
TabOrder = 3
object cbAvailableCountries: TComboBox
Left = 16
Top = 24
Width = 145
Height = 21
Style = csDropDownList
TabOrder = 0
end
end
end
6 changes: 6 additions & 0 deletions MainView.pas
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ TMainViewForm = class(TForm)
[Bind('Enabled', 'ChosenTicket')]
btnClear: TButton;

GroupBox4: TGroupBox;
[Bind('Value', 'Country')]
[BindOptions('AvailableCountries')]
[BindOptionsCaption('Choose...')]
cbAvailableCountries: TComboBox;

procedure FormCreate(Sender: TObject);
end;

Expand Down
15 changes: 12 additions & 3 deletions MainViewModel.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ TViewModel = class(TComponent)
fHasClickedTooManyTimes: IObservable<Boolean>;
fChosenTicket: IObservable<TTicket>;
fTickets: TList<TTicket>;
fAvailableCountries: TArray<string>;
fCountry: IObservable<string>;

function GetLastName: string;
procedure SetLastName(const value: string);
Expand All @@ -48,6 +50,9 @@ TViewModel = class(TComponent)

property ChosenTicket: IObservable<TTicket> read fChosenTicket;
property Tickets: TList<TTicket> read fTickets;

property AvailableCountries: TArray<string> read fAvailableCountries;
property Country: IObservable<string> read fCountry;
end;

implementation
Expand Down Expand Up @@ -76,20 +81,24 @@ constructor TViewModel.Create(const firstName, lastName: string);
end);

// Example 2
fNumberOfClicks := TObservable<Integer>.Create(0);
fNumberOfClicks := TObservable<Integer>.Create;
fHasClickedTooManyTimes := TDependentObservable<Boolean>.Create(
function: Boolean
begin
Result := fNumberOfClicks.Value >= 3;
end);

// Example 3
fChosenTicket := TObservable<TTicket>.Create(nil);
fTickets := TObjectList<TTicket>.Create();
fChosenTicket := TObservable<TTicket>.Create;
fTickets := TObjectList<TTicket>.Create;
fTickets.AddRange([
TTicket.Create('Economy', 199.95),
TTicket.Create('Business', 449.22),
TTicket.Create('First Class', 1199.99)]);

// Example 4
fAvailableCountries := TArray<string>.Create('AU', 'NZ', 'US');
fCountry := TObservable<string>.Create;
end;

destructor TViewModel.Destroy;
Expand Down
20 changes: 17 additions & 3 deletions SimpleMVVM.Binding.Components.pas
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ constructor TBinding<T>.Create(const component: T; const observable: IObservable
begin
inherited Create(component);
fComponent := component;
fObservable := TDependentObservable<TValue>.Create(
fObservable := TDependentObservable.Create(
Initialize(observable),
procedure(const value: TValue)
begin
Expand Down Expand Up @@ -246,11 +246,19 @@ function TEditBinding.Initialize(const observable: IObservable): TFunc<TValue>;
{$REGION 'TComboBoxBinding'}

procedure TComboBoxBinding.HandleChange(Sender: TObject);
var
o: TObject;
begin
if fComponent.ItemIndex = -1 then
fObservable.Value := nil
else
fObservable.Value := fComponent.Items.Objects[fComponent.ItemIndex];
begin
o := fComponent.Items.Objects[fComponent.ItemIndex];
if o = nil then
fObservable.Value := fComponent.Items[fComponent.ItemIndex]
else
fObservable.Value := o;
end;
end;

procedure TComboBoxBinding.InitComponent;
Expand All @@ -262,8 +270,14 @@ function TComboBoxBinding.Initialize(const observable: IObservable): TFunc<TValu
begin
Result :=
function: TValue
var
value: TValue;
begin
fComponent.ItemIndex := fComponent.Items.IndexOfObject(observable.Value.AsObject);
value := observable.Value;
if value.IsObject then
fComponent.ItemIndex := fComponent.Items.IndexOfObject(value.AsObject)
else
fComponent.ItemIndex := fComponent.Items.IndexOf(value.ToString);
end;
end;

Expand Down
Loading

0 comments on commit a12ecb1

Please sign in to comment.