comparison Lib/IMPL/Web/Application/ResourceContract.pm @ 332:04a093f0a5a6

IMPL::Web::Application refactoring: resources are created per client request
author cin
date Sun, 09 Jun 2013 21:48:57 +0400
parents 2ff1726c066c
children
comparison
equal deleted inserted replaced
331:2ff1726c066c 332:04a093f0a5a6
119 OperationContract => 'IMPL::Web::Application::OperationContract' 119 OperationContract => 'IMPL::Web::Application::OperationContract'
120 }; 120 };
121 121
122 my $contract = ResourceContract->new( 122 my $contract = ResourceContract->new(
123 verbs => { 123 verbs => {
124 get => OperationContract->new( 124 get => sub {
125 my ($resource,$action) = @_;
126 return "Hello!";
127 },
128 post => OperationContract->new(
125 binding => sub { 129 binding => sub {
126 my ($resource,$action) = @_; 130 my ($resource,$action) = @_;
127 return "Hello!"; 131 my $data = My::App::ModelBinder->new($resource->ds)->Bind($action);
128 }
129 ),
130 post => OperationContract->new(
131 parameters => [
132 IMPL::Transform::DataToModel->new() # создаем преобразование для формы
133 ],
134 binding => sub {
135 my ($resource,$action,$data) = @_;
136 return $resource->model->AddItem($data);
137 },
138 success => sub {
139 my ($resource,$action,$result) = @_;
140 return IMPL::Web::HttpResponse->Redirect( 132 return IMPL::Web::HttpResponse->Redirect(
141 location => $resource->location->Child($result->id) 133 location => $resource->location->Child($result->id)
142 ) 134 )
143 },
144 error => sub {
145 my ($resource,$action,$error) = @_;
146 $action->errors->Append($error);
147 return $resource->model;
148 } 135 }
149
150 ), 136 ),
151 }, 137 },
152 resources => [ 138 resources => [
153 { 139 {
154 name => 'info', 140 name => 'info',
155 binding => sub { 141 binding => sub {
156 return $_[0]->model->info; 142 return $_[0]->model->info;
157 }, 143 },
158 contract => ResourceContract->new( 144 contract => {
159 verbs => { 145 verbs => {
160 # using method references is also possible
161 get => sub { 146 get => sub {
162 my ($resource,$action) = @_; 147 my ($resource,$action) = @_;
163 return $resource->model; 148 return $resource->model;
164 } 149 }
165 150
166 } 151 }
167 ) 152 }
168 } 153 }
169 ] 154 ]
170 ) 155 )
171 156
172 my $obj = My::App::Data->fetch('something'); 157 my $obj = My::App::Data->fetch('something');
173 158
174 my $resource = $contract->CreateResource( 159 my $resource = $contract->CreateResource(
160 request => $currentRequest,
175 model => $obj, 161 model => $obj,
176 parent => $prentResource, 162 parent => $prentResource,
177 id => 'item-something' 163 id => 'item-something'
178 ); 164 );
179 165