-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6dd23ca
Showing
5 changed files
with
1,567 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
object MainForm: TMainForm | ||
Left = 0 | ||
Top = 0 | ||
Caption = 'SimpleMVVMDemo' | ||
ClientHeight = 289 | ||
ClientWidth = 561 | ||
Color = clBtnFace | ||
Font.Charset = DEFAULT_CHARSET | ||
Font.Color = clWindowText | ||
Font.Height = -11 | ||
Font.Name = 'Tahoma' | ||
Font.Style = [] | ||
OldCreateOrder = False | ||
OnCreate = FormCreate | ||
PixelsPerInch = 96 | ||
TextHeight = 13 | ||
object GroupBox1: TGroupBox | ||
Left = 8 | ||
Top = 8 | ||
Width = 161 | ||
Height = 121 | ||
Caption = 'Example 1' | ||
TabOrder = 0 | ||
object lblFullName: TLabel | ||
Left = 16 | ||
Top = 100 | ||
Width = 53 | ||
Height = 13 | ||
Caption = 'lblFullName' | ||
end | ||
object edtFirstName: TEdit | ||
Left = 16 | ||
Top = 24 | ||
Width = 121 | ||
Height = 21 | ||
TabOrder = 0 | ||
Text = 'edtFirstName' | ||
end | ||
object edtLastName: TEdit | ||
Left = 16 | ||
Top = 65 | ||
Width = 121 | ||
Height = 21 | ||
TabOrder = 1 | ||
Text = 'edtLastName' | ||
end | ||
end | ||
object GroupBox2: TGroupBox | ||
Left = 184 | ||
Top = 8 | ||
Width = 169 | ||
Height = 169 | ||
Caption = 'Example 2' | ||
TabOrder = 1 | ||
object lblClickCount: TLabel | ||
Left = 18 | ||
Top = 27 | ||
Width = 60 | ||
Height = 13 | ||
Caption = 'lblClickCount' | ||
end | ||
object lblClickedTooManyTimes: TLabel | ||
Left = 18 | ||
Top = 77 | ||
Width = 113 | ||
Height = 13 | ||
Caption = 'Clicked too many times!' | ||
end | ||
object btnRegisterClick: TButton | ||
Left = 18 | ||
Top = 46 | ||
Width = 75 | ||
Height = 25 | ||
Caption = 'Click me' | ||
TabOrder = 0 | ||
end | ||
object btnResetClicks: TButton | ||
Left = 18 | ||
Top = 111 | ||
Width = 75 | ||
Height = 25 | ||
Caption = 'Reset clicks' | ||
TabOrder = 1 | ||
end | ||
end | ||
object GroupBox3: TGroupBox | ||
Left = 368 | ||
Top = 8 | ||
Width = 185 | ||
Height = 169 | ||
Caption = 'Example 3' | ||
TabOrder = 2 | ||
object lblPrice: TLabel | ||
Left = 16 | ||
Top = 68 | ||
Width = 33 | ||
Height = 13 | ||
Caption = 'lblPrice' | ||
end | ||
object cbTickets: TComboBox | ||
Left = 16 | ||
Top = 24 | ||
Width = 145 | ||
Height = 21 | ||
Style = csDropDownList | ||
TabOrder = 0 | ||
end | ||
object btnClear: TButton | ||
Left = 88 | ||
Top = 51 | ||
Width = 75 | ||
Height = 25 | ||
Caption = 'Clear' | ||
TabOrder = 1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
unit Main; | ||
|
||
interface | ||
|
||
uses | ||
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, | ||
Dialogs, StdCtrls, SimpleMVVM; | ||
|
||
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 | ||
Generics.Collections; | ||
|
||
type | ||
TTicket = class | ||
private | ||
fName: string; | ||
fPrice: Currency; | ||
public | ||
constructor Create(const name: string; price: Currency); | ||
property Name: string read FName write FName; | ||
property Price: Currency read FPrice write FPrice; | ||
end; | ||
|
||
TViewModel = class(TComponent) | ||
private | ||
fLastName: IObservable<string>; | ||
fFirstName: IObservable<string>; | ||
fFullName: IObservable<string>; | ||
fNumberOfClicks: IObservable<Integer>; | ||
fHasClickedTooManyTimes: IObservable<Boolean>; | ||
fChosenTicket: IObservable<TTicket>; | ||
fTickets: TList<TTicket>; | ||
|
||
function GetLastName: string; | ||
procedure SetLastName(const value: string); | ||
public | ||
constructor Create(const firstName, lastName: string); reintroduce; | ||
destructor Destroy; override; | ||
|
||
procedure RegisterClick; | ||
procedure ResetClicks; | ||
|
||
procedure ResetTicket; | ||
|
||
property LastName: string read GetLastName write SetLastName; | ||
property FirstName: IObservable<string> read fFirstName; | ||
property FullName: IObservable<string> read fFullName; | ||
|
||
property NumberOfClicks: IObservable<Integer> read fNumberOfClicks; | ||
property HasClickedTooManyTimes: IObservable<Boolean> read fHasClickedTooManyTimes; | ||
|
||
property ChosenTicket: IObservable<TTicket> read fChosenTicket; | ||
property Tickets: TList<TTicket> read fTickets; | ||
end; | ||
|
||
{ TTicket } | ||
|
||
constructor TTicket.Create(const name: string; price: Currency); | ||
begin | ||
fName := name; | ||
fPrice := price; | ||
end; | ||
|
||
{ TViewModel } | ||
|
||
constructor TViewModel.Create(const firstName, lastName: string); | ||
begin | ||
inherited Create(nil); | ||
|
||
// Example 1 | ||
fLastName := TObservable<string>.Create(lastName); | ||
fFirstName := TObservable<string>.Create(firstName); | ||
fFullName := TDependentObservable<string>.Create( | ||
function: string | ||
begin | ||
Result := fFirstName.Value + ' ' + fLastName.Value; | ||
end); | ||
|
||
// Example 2 | ||
fNumberOfClicks := TObservable<Integer>.Create(0); | ||
fHasClickedTooManyTimes := TDependentObservable<Boolean>.Create( | ||
function: Boolean | ||
begin | ||
Result := fNumberOfClicks.Value >= 3; | ||
end); | ||
|
||
// Example 3 | ||
fChosenTicket := TObservable<TTicket>.Create(nil); | ||
fTickets := TObjectList<TTicket>.Create(); | ||
fTickets.AddRange([ | ||
TTicket.Create('Economy', 199.95), | ||
TTicket.Create('Business', 449.22), | ||
TTicket.Create('First Class', 1199.99)]); | ||
end; | ||
|
||
destructor TViewModel.Destroy; | ||
begin | ||
fTickets.Free; | ||
inherited; | ||
end; | ||
|
||
function TViewModel.GetLastName: string; | ||
begin | ||
Result := fLastName.Value; | ||
end; | ||
|
||
procedure TViewModel.RegisterClick; | ||
begin | ||
fNumberOfClicks.Value := fNumberOfClicks.Value + 1; | ||
end; | ||
|
||
procedure TViewModel.ResetClicks; | ||
begin | ||
fNumberOfClicks.Value := 0; | ||
end; | ||
|
||
procedure TViewModel.ResetTicket; | ||
begin | ||
fChosenTicket.Value := nil; | ||
end; | ||
|
||
procedure TViewModel.SetLastName(const value: string); | ||
begin | ||
fLastName.Value := value; | ||
end; | ||
|
||
{ TMainForm } | ||
|
||
procedure TMainForm.FormCreate(Sender: TObject); | ||
begin | ||
ApplyBindings(Self, TViewModel.Create('John', 'Doe')); | ||
end; | ||
|
||
end. |
Oops, something went wrong.