Skip to content

Commit

Permalink
moved viewmodel into its own unit
Browse files Browse the repository at this point in the history
  • Loading branch information
sglienke committed Jun 7, 2015
1 parent 89be1fb commit 5b86e99
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Main.dfm → MainView.dfm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object MainForm: TMainForm
object MainViewForm: TMainViewForm
Left = 0
Top = 0
Caption = 'SimpleMVVMDemo'
Expand Down
64 changes: 64 additions & 0 deletions MainView.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
unit MainView;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SimpleMVVM.Binding;

type
TMainViewForm = class(TForm)

GroupBox1: TGroupBox;
[Bind('Value', 'FirstName')]
edtFirstName: TEdit;
[Bind('Value', 'LastName')]
edtLastName: TEdit;
[Bind('Text', 'FullName')]
lblFullName: TLabel;

GroupBox2: TGroupBox;
[Bind('Click', 'RegisterClick')]
[Bind('Disabled', 'HasClickedTooManyTimes')]
btnRegisterClick: TButton;
[Bind('Text', 'NumberOfClicks')]
lblClickCount: TLabel;
[Bind('Click', 'ResetClicks')]
[Bind('Visible', 'HasClickedTooManyTimes')]
btnResetClicks: TButton;
[Bind('Visible', 'HasClickedTooManyTimes')]
lblClickedTooManyTimes: TLabel;

GroupBox3: TGroupBox;
[Bind('Value', 'ChosenTicket')]
[BindOptions('Tickets')]
[BindOptionsCaption('Choose...')]
[BindOptionsText('Name')]
cbTickets: TComboBox;
[Bind('Text', 'ChosenTicket.Price')]
lblPrice: TLabel;
[Bind('Click', 'ResetTicket')]
[Bind('Enabled', 'ChosenTicket')]
btnClear: TButton;

procedure FormCreate(Sender: TObject);
end;

var
MainViewForm: TMainViewForm;

implementation

{$R *.dfm}

uses
MainViewModel;

{ TMainForm }

procedure TMainViewForm.FormCreate(Sender: TObject);
begin
ApplyBindings(Self, TViewModel.Create('John', 'Doe'));
end;

end.
61 changes: 4 additions & 57 deletions Main.pas → MainViewModel.pas
Original file line number Diff line number Diff line change
@@ -1,57 +1,9 @@
unit Main;
unit MainViewModel;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SimpleMVVM.Binding;

type
TMainForm = class(TForm)

GroupBox1: TGroupBox;
[Bind('Value', 'FirstName')]
edtFirstName: TEdit;
[Bind('Value', 'LastName')]
edtLastName: TEdit;
[Bind('Text', 'FullName')]
lblFullName: TLabel;

GroupBox2: TGroupBox;
[Bind('Click', 'RegisterClick')]
[Bind('Disabled', 'HasClickedTooManyTimes')]
btnRegisterClick: TButton;
[Bind('Text', 'NumberOfClicks')]
lblClickCount: TLabel;
[Bind('Click', 'ResetClicks')]
[Bind('Visible', 'HasClickedTooManyTimes')]
btnResetClicks: TButton;
[Bind('Visible', 'HasClickedTooManyTimes')]
lblClickedTooManyTimes: TLabel;

GroupBox3: TGroupBox;
[Bind('Value', 'ChosenTicket')]
[BindOptions('Tickets')]
[BindOptionsCaption('Choose...')]
[BindOptionsText('Name')]
cbTickets: TComboBox;
[Bind('Text', 'ChosenTicket.Price')]
lblPrice: TLabel;
[Bind('Click', 'ResetTicket')]
[Bind('Enabled', 'ChosenTicket')]
btnClear: TButton;

procedure FormCreate(Sender: TObject);
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

uses
Classes,
Generics.Collections,
SimpleMVVM.Observable;

Expand Down Expand Up @@ -98,6 +50,8 @@ TViewModel = class(TComponent)
property Tickets: TList<TTicket> read fTickets;
end;

implementation

{ TTicket }

constructor TTicket.Create(const name: string; price: Currency);
Expand Down Expand Up @@ -169,11 +123,4 @@ procedure TViewModel.SetLastName(const value: string);
fLastName.Value := value;
end;

{ TMainForm }

procedure TMainForm.FormCreate(Sender: TObject);
begin
ApplyBindings(Self, TViewModel.Create('John', 'Doe'));
end;

end.
2 changes: 1 addition & 1 deletion SimpleMVVM.Binding.pas
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ procedure ApplyBindings(const view: TComponent; const viewModel: TObject);
f: TRttiField;
a: TCustomAttribute;
begin
if (viewModel is TComponent) then
if viewModel is TComponent then
if TComponent(viewModel).Owner = nil then
view.InsertComponent(TComponent(viewModel));

Expand Down
7 changes: 3 additions & 4 deletions SimpleMVVMDemo.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ program SimpleMVVMDemo;

uses
Forms,
Main in 'Main.pas' {MainForm},
SimpleMVVM.Binding in 'SimpleMVVM.Binding.pas',
SimpleMVVM.Binding.Components in 'SimpleMVVM.Binding.Components.pas';
MainView in 'MainView.pas' {MainViewForm},
MainViewModel in 'MainViewModel.pas';

{$R *.res}

begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TMainViewForm, MainViewForm);
Application.Run;
ReportMemoryLeaksOnShutdown := True;
end.
Loading

0 comments on commit 5b86e99

Please sign in to comment.